[英]mouseover, mouseout, click event handler not firing
Here is the code - 这是代码-
setEvents:function(a, b){
if(b) {
$('#id').mouseover(function(){
console.log('mouse over')
})
$('#id').mouseout(function(){
console.log('mouseout')
})
$('#id').click(function(){
console.log('click')
})
}
}
After this function is executed, I expect that the respective handlers will be fired on the events. 执行此函数后,我希望相应的处理程序将在事件上触发。 But It is not happening.
但这没有发生。 However, if I put aa dbugger in the method, and trigger the handlers after it has attached, it works.
但是,如果我将dbugger放入方法中,并在附加后触发处理程序,则它会起作用。 That is when I am inside the method it works.
那就是我在方法内部的时候。 But It does not work once the method is executed.
但是,一旦执行该方法,它将不起作用。
You have to bind that function to an object, then there must be a call to the declared function in order to register the event handlers for the #id
element: 您必须将该函数绑定到一个对象,然后必须对已声明的函数进行调用才能注册
#id
元素的事件处理程序:
var handler = {
setEvents:function(a, b){
if(b) {
$('#id').mouseover(function(){
console.log('mouse over')
})
$('#id').mouseout(function(){
console.log('mouseout')
})
$('#id').click(function(){
console.log('click')
})
}
}
}
handler.setEvents(a, b);
Here you can take a look to a complete Demo on JSFiddle . 在这里,您可以看一下JSFiddle上的完整演示 。
Try this ... jsFiddle Demo 试试这个... jsFiddle演示
HTML (example): HTML(示例):
<div id="id">lala (with #id)</div>
<br/>
<div>lele (without #id)</div>
JS: JS:
var setEvents = function(a,b){
if(b){
$('#id').mouseover(function(){
console.log('mouse over');
});
$('#id').mouseout(function(){
console.log('mouseout');
});
$('#id').click(function(){
console.log('click');
});
}
}
$(function(){
setEvents(1,1);
});
You didn't post full code "how to call setEvents". 您没有发布完整的代码“如何调用setEvents”。 But i think maybe you didn't call it when DOM is ready.
但是我认为也许在DOM准备就绪时您没有调用它。
$( document ).ready(function() {
// Call setEvents
});
Hope it'll help, if it doesn't work, please post more code 希望它会有所帮助,如果它不起作用,请发布更多代码
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.