简体   繁体   English

如何在href之前触发onClick

[英]How to trigger onClick before href

I am trying to do some functionality in onClick() event and also doing the href for the same element. 我试图在onClick()事件中执行一些功能,并为同一元素执行href But actually happening is its not triggering onClick() completely and its moving to href link when we click that element. 但实际上发生的是它不完全触发onClick() ,当我们点击该元素时它会转移到href链接。

How to make onClick() gets complete first and trigger href next. 如何让onClick()先完成并触发href

<a href="" onclick="javascript:triggerMe()">click</a>

据我所知,你不想去其他页面,所以使用return false,如下所示:

<a href="" onclick="javascript:triggerMe();return false;">click</a>

My best advice is to try using a form as follows: 我最好的建议是尝试使用如下表格

First, change your href to: 首先,将您的href更改为:

<a href="#null" onclick="javascript:triggerMe()">click</a>

and add somewhere in your HTML code a form with a display=none button: 并在HTML代码中的某处添加一个带有display = none按钮的表单:

<form action="http://www.myhref.com"><button id="GoToHref" style="display:none"></button></form>

where action is your desired href. 动作是你想要的href。 The onclick event calls the triggerMe() function. onclick事件调用triggerMe()函数。 Change your function to include: 更改您的功能包括:

function triggerMe() {
    var DelayhrefCall;
    //include all code for what triggerMe() must do, then as the last line add
    DelayhrefCall = window.setTimeout(hrefCall, 500); 
    //500 millisecond delay, change time as desired
}

After a 500 ms delay, hrefCall() is called: 延迟500毫秒后,调用hrefCall():

function hrefCall() {
document.getElementById('GoToHref').click();
}

The display:none button inside the form is then clicked, triggering the form's action, which takes you to your desired href. 然后单击窗体内的显示:无按钮,触发窗体的动作,将您带到所需的href。 If you are linking to other pages on your site it works perfectly. 如果您要链接到您网站上的其他页面,那么它的效果非常好。 If you are linking to external pages you will need to add a target to your form: 如果要链接到外部页面,则需要在表单中添加目标:

<form action="http://www.myhref.com" target="_blank"><button id="GoToHref"    
style="display:none"></button></form>

You have to return a false value if you want to abort the effect of href. 如果要中止href的效果,则必须返回false值。 Try using this code. 尝试使用此代码。

<a href="yourLink.com" title="my link" onclick="return triggerMe();"> Click me </a>

Javascript part Javascript部分

<script>
function triggerMe()
{
    if (condition) 
    {
        //do stuffs
        //This will tell browser to follow the link.
        return true;
    } 
    else 
    {
        //do stuffs
        //This will cancel the default behaviour of the browser.
        //Abort the effect of href
        return false;
    }
}
</script>
<a  onclick="javascript:triggerMe()" href="">click</a>

它会工作

 <a href="" onclick="return triggerme()" >click</a>
 function triggerme(e){
    if(condition is true){
       return true;
    } else {
       e.preventDefault();
       return false;
    }  
 }

Try this one will surely work 试试这个肯定会奏效

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

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