javascript jQuery .bind() 无法正常工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6171985/
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
jQuery .bind() not working properly
提问by Casey Flynn
I'm attempting to bind functions to these events using jQuery:
我正在尝试使用 jQuery 将函数绑定到这些事件:
$("#cmplist tr").bind('onmouseover', function(){
console.log('1');
});
$("#cmplist tr").bind('onmouseout', function(){
console.log('2');
});
$("#cmplist tr").bind('click', function(){
console.log('3');
});
However none of them seem to work. I'm reasonably sure my selectors are correct since when I enter into the console $("#cmplist tr")
it returns:
然而,它们似乎都不起作用。我有理由确定我的选择器是正确的,因为当我进入控制台时$("#cmplist tr")
它返回:
[tr, tr#survey3, tr#survey20, tr#survey22, tr#survey26, tr#survey28, tr#survey29, tr#survey30, tr#survey33, tr#survey34, tr#survey6, tr#survey19, tr#survey14, tr#survey32, tr#sweepstakes5, tr#sweepstakes9, tr#coupons5, tr#freesample4, tr#freesample5, tr#freesample6, tr#freesample7, tr#gifts3, tr#gifts4, tr#polls2, tr#polls5, tr#polls6, tr#quiz8, tr#trivia4, tr#photo6, tr#photo10, tr#photo11, tr#photo12, tr#photo13, tr#photo15, tr#photo16, tr#photo17, tr#video4, tr#iframe2, tr#iframe4]
Am I missing something about how jQuery events work?
我是否缺少有关 jQuery 事件如何工作的信息?
回答by Pointy
Drop the "on" from your event names. Also I think the hip thing to do nowadays is to use "mouseenter" and "mouseleave" with jQuery event handlers. Those events are "normalized" by the library so as to make everything more reliable in the face of quirky browser behavior.
从您的事件名称中删除“on”。此外,我认为现在最流行的做法是在 jQuery 事件处理程序中使用“mouseenter”和“mouseleave”。这些事件由库“规范化”,以便在面对古怪的浏览器行为时使一切更加可靠。
Also, make sure you do your binding in a "ready" handler, unless you're confident you've got an alternative, equally effective solution:
另外,请确保在“就绪”处理程序中进行绑定,除非您确信自己有替代的、同样有效的解决方案:
$(function() {
// your event binding stuff here
});
回答by thecodeparadox
try this:
试试这个:
$(function(){
$("#cmplist tr").bind('mouseover', function(){
console.log('1');
});
$("#cmplist tr").bind('mouseout', function(){
console.log('2');
});
$("#cmplist tr").bind('click', function(){
console.log('3');
});
});