2013-08-19 83 views
0

我对标签有点击功能。我想在同一个元素上绑定mouseover事件。这可能与.bind方法。 fiddle在jquery中绑定事件

$(function(){ 
    $('a').click(function(){ 
     alert(0); 
    }) 
    $('a').bind('mouseover') 
}) 
<a href="#">Jitender</a> 
+0

你的意思是你想在'click'和'mouseover'上运行相同的事件处理程序吗? –

+0

@RoryMcCrossan:是的 – Carlos

+0

没问题,请参阅下面的答案。 –

回答

1

是的。只要绑定之后点击绑定鼠标悬停:

$('a').click(function(){ 
    alert(0); 
}).bind('mouseover', function() { 
    $(this).css('background-color', 'red'); // To show it working 
}); 

http://jsfiddle.net/R7qrC/3/

0

是的,是这样的:

Fiddle

$('a').bind('mouseover', function() { 
    alert(0); 
}); 

此外,bind()是过时的,如果你正在使用jQuery的新版本(1.7+)您应该使用on()

因为它很难看到两个鼠标悬停并单击创建警报(因为从mouseover警报会阻止你点击它)事件,下面将让你看到这两个事件更好的工作:

Fiddle

$('a').on('mouseover click', function(){ 
    $(this).toggleClass("test"); 
}); 
1

您应该使用on关键字。

$('a').on('mouseover', function() { alert(1);}) 

jQuery documentation

“在jQuery 1.7中,。对()方法是事件处理程序的 附着于文档的首选方法。”

3

假设你要绑定的相同的处理到clickmouseover事件中,你可以试试这个:

$('a').on('click mouseover', function(e) { 
    e.preventDefault(); 
    alert('0'); 
}); 

注的用法on优于jQuery 1.7+中的bind

2
$(function(){ 
    $('a').on('click mouseover', function() { 
     alert(0); 
     return false; 
    });  
}); 
+0

它不起作用 – Carlos

+0

代码已更新。再试一次 – Shivam