Javascript 调用两个 javascripts 函数 onClick
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3210645/
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
Calling two javascripts functions onClick
提问by ayush
Presently i have the following code on one of my web page-
目前,我的其中一个网页上有以下代码-
<a href="http://ex.com" onclick="return popitup2()">Grab Coupon</a>
now I want to run one more script which is used in the following way -
现在我想再运行一个脚本,它的使用方式如下-
onClick="recordOutboundLink(this, 'Outbound Links', 'ex.com');return false;"
Now can someone tell me how do i call both of these javacsripts when the link is clicked. Thanks in advance.
现在有人可以告诉我单击链接时如何调用这两个 javacsript。提前致谢。
回答by Darin Dimitrov
You can call the two functions in the onclick event handler:
您可以在 onclick 事件处理程序中调用这两个函数:
<a href="http://ex.com" onclick="popitup2(); recordOutboundLink(this, 'Outbound Links', 'ex.com'); return false;">Grab Coupon</a>
To avoid mixing markup with javascript I would recommend you attaching the onclickevent for this particular link like this:
为了避免将标记与 javascript 混合,我建议您onclick为这个特定链接附加事件,如下所示:
<a href="http://ex.com" id="mylink">Grab Coupon</a>
And in the headsection:
在该head部分:
<script type="text/javascript">
window.onload = function() {
var mylink = document.getElementById('mylink');
if (mylink != null) {
mylink.onclick = function() {
var res = popitup2();
recordOutboundLink(this, 'Outbound Links', 'ex.com');
return res;
};
}
};
</script>
回答by Sarfraz
Specify both of them in your link:
在您的链接中指定它们:
<a href="http://ex.com" onclick="recordOutboundLink(this, 'Outbound Links', 'ex.com'); return popitup2();">Grab Coupon</a>
回答by Mark Elliot
You can do it with a closure:
你可以用闭包来做到这一点:
<a href="http://ex.com" onclick="return function(){ recordOutboundLink(this, 'Outbound Links', 'ex.com'); return popitup2(); }()">Grab Coupon</a>
or just some better ordering:
或者只是一些更好的排序:
<a href="http://ex.com" onclick="recordOutboundLink(this, 'Outbound Links', 'ex.com');return popitup2();">Grab Coupon</a>

