简体   繁体   English

如何在jQuery中绑定多个事件

[英]How to bind multiple event in jQuery

I tried to bind multiple events in jQuery.我试图在 jQuery 中绑定多个事件。

For example in the below snippet, I defined 2 functions and they are used in eventhander .例如,在下面的代码片段中,我定义了2 functions ,它们在eventhander中使用。

I tried this code but didn't work well.我试过这段代码,但效果不佳。

I would like to know where must be fixed.我想知道必须修复的地方。

If someone has opinion please let me know.如果有人有意见,请告诉我。

Thanks谢谢

 function outpatient(elm) { elm.removeClass().addClass(style1); } function hover(elm) { elm.addClass(style2); } $("#calendar .day").on("click hover", function(event) { if (event.type == "click") { outpatient($(this)) } else { hover($(this)) } });
 td { padding: 10px; border: solid black 1px; } table { border-collapse: collapse; } .is-clicked { background-color: aqua; } .style1 { background-color: red; } .style2 { background-color: aqua; }
 <script src="https://code.jquery.com/jquery-3.3.1.js"></script> <div id=calendar> <table> <tr> <td id=1 class=day>1</td> <td id=2 class=day>2</td> <td id=3 class=day>3</td> <td id=4 class=day>4</td> <td id=5 class=day>5</td> <td id=6 class=day>6</td> <td id=7 class=day>7</td> </tr> </table> </div>

Listen for the mouseenter event instead, there's no such thing as a hover event:而是监听mouseenter事件,没有hover事件之类的东西:

 function outpatient(elm) { elm.removeClass().addClass('style1'); } function hover(elm) { elm.addClass('style2'); } $("#calendar .day").on("click mouseenter", function(event) { if (event.type == "click") { outpatient($(this)) } else { hover($(this)) } });
 td { padding: 10px; border: solid black 1px; } table { border-collapse: collapse; } .is-clicked { background-color: aqua; } .style1 { background-color: red; } .style2 { background-color: aqua; }
 <script src="https://code.jquery.com/jquery-3.3.1.js"></script> <div id=calendar> <table> <tr> <td id=1 class=day>1</td> <td id=2 class=day>2</td> <td id=3 class=day>3</td> <td id=4 class=day>4</td> <td id=5 class=day>5</td> <td id=6 class=day>6</td> <td id=7 class=day>7</td> </tr> </table> </div>

You can simplify your code a little by passing an object with the events you want to listen for as the keys, and their associated functions you want to execute when called.您可以通过传递一个对象作为键,其中包含您想要侦听的事件,以及您想要在调用时执行的相关函数,从而稍微简化您的代码。 You can use $(this) to reference the element triggering the event in your functions:您可以使用$(this)来引用在您的函数中触发事件的元素:

 function outpatient() { $(this).removeClass().addClass('style1'); } function hover() { $(this).addClass('style2'); } $("#calendar .day").on({ mouseenter: hover, /* mouseover or mouseenter for hover */ click: outpatient });
 td { padding: 10px; border: solid black 1px; } table { border-collapse: collapse; } .is-clicked { background-color: aqua; } .style1 { background-color: red; } .style2 { background-color: aqua; }
 <script src="https://code.jquery.com/jquery-3.3.1.js"></script> <div id=calendar> <table> <tr> <td id=1 class=day>1</td> <td id=2 class=day>2</td> <td id=3 class=day>3</td> <td id=4 class=day>4</td> <td id=5 class=day>5</td> <td id=6 class=day>6</td> <td id=7 class=day>7</td> </tr> </table> </div>

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

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