使用 JQuery/Javascript 在单个事件上执行多个函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4951388/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 18:15:50  来源:igfitidea点击:

Multiple Functions on a Single Event with JQuery/Javascript

jquery

提问by Rrryyyaaannn

What is the best way to call two functions upon a single click event?

在单击事件上调用两个函数的最佳方法是什么?

There will be multiple elements requiring this script, so the script must act only upon the element being clicked — not all the elements at once.

将有多个元素需要此脚本,因此脚本必须仅对被单击的元素起作用——而不是同时对所有元素起作用。

Let me know if I need to provide more details. Thanks again for all your help.

如果我需要提供更多详细信息,请告诉我。再次感谢你的帮助。

回答by Luca Filosofi

$(function() {
    $('a').click(function() {
        func1();
        func2();
    });
});

回答by Jason Benson

jQuery will handle that for you quite nicely, see example:

jQuery 会很好地为您处理,请参见示例:

$('div').click(function(){
    alert($(this).text()+' clicked');
})
.click(function(){
    $(this).css({'color':'#ff0000'});
});
<div>Button 1</div>
<div>Button 2</div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

Here's a jsFiddle example: http://www.jsfiddle.net/pcASq/

这是一个 jsFiddle 示例:http: //www.jsfiddle.net/pcASq/