javascript 在javascript函数中模拟href点击?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12852549/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 17:16:10  来源:igfitidea点击:

Simulate the href click in javascript function?

javascriptjakarta-eejavascript-eventshref

提问by M Sach

I have below code in my legacy project in jsp

我在jsp的遗留项目中有以下代码

   <tr>
    <th colspan="2">Customer</th>
    <td colspan="2">
      <a href="myAction.do" target="view">CustomerLink</a></td>
  </tr>

I want little modification in behaviour when we click CustomerLink. i just want to do some processing in javascript function before hitting the action. I am not getting how to simulate the href link behaviour in javascript function?

当我们单击 CustomerLink 时,我希望对行为进行一些修改。我只想在点击操作之前在 javascript 函数中做一些处理。我不知道如何在 javascript 函数中模拟 href 链接行为?

What i Tried:-

我试过的:-

 <tr>
    <th colspan="2">Customer</th>
    <td colspan="2">
      <a href="javascript:customerLinkClicked('myAction.do')" target="view">CustomerLink</a></td>
 </tr>

// javascript function

// javascript 函数

    function customerLinkClicked(path)
    {

        // simulate the href link behaviour, not sure how to do this


    }

回答by y34h

<a href="javascript:customerLinkClicked('myAction.do')" target="view">CustomerLink</a>
<script>
    function customerLinkClicked(path) {
        if (confirm('message'))
            document.location.href = path;
    }
</script>

回答by Klaster_1

You can use window.confirmfor getting confirmation dialog, which returns true or false, depending on choice. After that, default behaviour may be cancelled by event.preventDefaultmethod.

您可以使用window.confirm获取确认对话框,根据选择返回 true 或 false。之后,可以通过event.preventDefault方法取消默认行为。

Example: http://jsfiddle.net/Klaster_1/nXtvF/

示例:http: //jsfiddle.net/Klaster_1/nXtvF/

$("a").on("click", function(event) {
    if(!window.confirm("Proceed?")){
        event.preventDefault()
    };
});

I've used jQuery in it, but it's totally optional.

我在其中使用了 jQuery,但它完全是可选的。

回答by Nippey

What about:

关于什么:

<a href="myAction.do" onclick="customerLinkClicked()" target="view">CustomerLink</a>

At first the onclick event handler will be processed, then the location will be changed to myAction.do.

首先会处理onclick 事件处理程序,然后将位置更改为myAction.do。

If you want to prevent changing the location, you can write

如果你想防止改变位置,你可以写

onclick="return customerLinkClicked();"

Depending on the return value (true, false), the location change will happen. Or not.

根据返回值(真、假),位置会发生变化。或不。

回答by polin

Try this.just copy and paste

试试这个。只需复制和粘贴

<script type="text/javascript">
function fnc()
{
if(confirm("Do you really want to go?"))
{window.open("yoursitename.do");}
}
</script
<a href="javascript:fnc()">click</a>

回答by Jashwant

You should avoid inline js, but for you current scenario.

您应该避免内联 js,但对于您当前的情况。

    function customerLinkClicked(event) {
        if(!confirm("Are you sure ?")) {
              if(event.preventDefault) {
                  event.preventDefault();
              }
              else {
                   event.returnValue = false;
              } 
        } 
    }

HTML

HTML

<a href="myAction.do" onclick="customerLinkClicked(event);" target="view">CustomerLink</a>