简体   繁体   English

两次调用OnClick-在标签中一次,在外部托管脚本中一次

[英]Calling OnClick Twice - Once in Tag and Once in Externally Hosted Script

I have a link that is tied to an externally hosted JS script that I cannot change. 我有一个链接到一个无法更改的外部托管JS脚本。 The external script uses jquery to bind to the click function to perform an action. 外部脚本使用jquery绑定到click函数以执行操作。

<script...src="external cannot change"></script>
<a class="clicker" href="link">Click me</a>

I need to perform another action onClick as well. 我还需要执行其他操作onClick。 But when I do the below, the browser only runs the onclick even bound in the external script. 但是,当我执行以下操作时,浏览器仅运行甚至绑定在外部脚本中的onclick。

<a class="clicker" href="link" onclick="doThis();">Click me</a>

It does not run doThis(). 它不运行doThis()。 It only performs the action specified in the externally hosted script. 它仅执行外部托管脚本中指定的操作。

Is there a method to run 2 onClick events - 1 stored in the external script and 1 coded locally? 有没有一种方法可以运行2个onClick事件-1个存储在外部脚本中,而1个在本地编码?

Try adding a click event with jQuery. 尝试使用jQuery添加click事件。 You can add as many of these as you want. 您可以根据需要添加任意多个。

$(".clicker").click(function() {
    //your code here
});

Try using a different event. 尝试使用其他事件。 onmouseup should work well. onmouseup应该工作良好。 If you want to catch the event and prevent the other script from doing the onclick, use onmousedown and then add 如果要捕获事件并阻止其他脚本执行onclick,请使用onmousedown然后添加

event.preventDefault();

Try using jQuery event binding instead of attribute based events. 尝试使用jQuery事件绑定而不是基于属性的事件。

function MyOwnClick(e){
  // do something.
}

$(".clicker").bind("click", MyOwnClick);

Remove the doThis() function for onclick.You can add another jquery function 删除onClick的doThis()函数。您可以添加另一个jquery函数

$(document).ready(function(){
$(".clicker").click(function(){
    //whatever you want to do
    });
});

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

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