Javascript 两个 onClick 操作一个按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10544520/
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
Two onClick actions one button
提问by jon de goof
Does someone know a wizards trick to make it work ?
有人知道巫师的技巧吗?
<input type="button" value="Dont show this again! " onClick="fbLikeDump();" onclick="WriteCookie();" />
PS: I am using it in a .js file.
PS:我在 .js 文件中使用它。
回答by xdazz
Additional attributes (in this case, the second onClick
) will be ignored. So, instead of onclick
calling both fbLikeDump();
and WriteCookie();
, it will only call fbLikeDump();
. To fix, simply define a single onclick
attribute and call both functions within it:
其他属性(在本例中为第二个onClick
)将被忽略。因此,它不会onclick
同时调用fbLikeDump();
and WriteCookie();
,而只会调用fbLikeDump();
。要修复,只需定义一个onclick
属性并在其中调用两个函数:
<input type="button" value="Don't show this again! " onclick="fbLikeDump();WriteCookie();" />
回答by Danilo Valente
Try it:
尝试一下:
<input type="button" value="Dont show this again! " onClick="fbLikeDump();WriteCookie();" />
Or also
或者也
<script>
function clickEvent(){
fbLikeDump();
WriteCookie();
}
</script>
<input type="button" value="Dont show this again! " onClick="clickEvent();" />
回答by Mirko
Give your button an id something like this:
给你的按钮一个像这样的id:
<input id="mybutton" type="button" value="Dont show this again! " />
Then use jquery (to make this unobtrusive) and attach click action like so:
然后使用 jquery(使其不显眼)并附加单击操作,如下所示:
$(document).ready(function (){
$('#mybutton').click(function (){
fbLikeDump();
WriteCookie();
});
});
(this part should be in your .js file too)
(这部分也应该在你的 .js 文件中)
I should have mentioned that you will need the jquery libraries on your page, so right before your closing body tag add these:
我应该提到您将需要页面上的 jquery 库,因此在结束正文标记之前添加以下内容:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript" src="http://PATHTOYOURJSFILE"></script>
The reason to add just before body closing tag is for performance of perceived page loading times
在正文结束标记之前添加的原因是为了感知页面加载时间的性能
回答by rfunduk
<input type="button" value="..." onClick="fbLikeDump(); WriteCookie();" />