简体   繁体   English

在Javascript函数中模拟href点击?

[英]Simulate the href click in Javascript function?

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.当我们单击 CustomerLink 时,我希望对行为进行一些修改。 I just want to do some processing in Javascript function before hitting the action.我只想在点击操作之前在 Javascript 函数中做一些处理。 I am not getting how to simulate the href link behaviour in Javascript function?我不知道如何在 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


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

You can use window.confirm for getting confirmation dialog, which returns true or false, depending on choice.您可以使用window.confirm获取确认对话框,根据选择返回 true 或 false。 After that, default behaviour may be cancelled by event.preventDefault method.之后,可以通过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,但它完全是可选的。

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.根据返回值(true、false),位置会发生变化。 Or not.或不。

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>

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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM