Javascript 使用JQuery设置div的mouseover属性

Javascript 使用JQuery设置div的mouseover属性,javascript,jquery,Javascript,Jquery,我想为div设置一个属性。我已经这样做了: $('#row-img_1').onmouseover = function (){ alert('foo'); }; $('#row-img_2').onmouseout = function (){ alert('foo2'); }; 但是,上述方法不起作用,当鼠标结束或移出时,它不会发出警报 我还尝试了$('row-img_1').attr()我也无法让它工作 我知道我应该使用更有效的事件处理系统,但我的div是动态生成的。此外,这是一个小项目

我想为div设置一个属性。我已经这样做了:

$('#row-img_1').onmouseover = function (){ alert('foo'); };
$('#row-img_2').onmouseout = function (){ alert('foo2'); };
但是,上述方法不起作用,当鼠标结束或移出时,它不会发出警报

我还尝试了
$('row-img_1').attr()我也无法让它工作

我知道我应该使用更有效的事件处理系统,但我的div是动态生成的。此外,这是一个小项目


谢谢大家的帮助。

事件注册为作为属性传递的函数,如下所示:

$('#row-img_1').mouseover(function (){ alert('foo'); });
$('#row-img_2').mouseout(function (){ alert('foo2'); });

另外,请注意
onmouseover
中缺少的
on
事件被注册为作为属性传递的函数,如下所示:

$('#row-img_1').mouseover(function (){ alert('foo'); });
$('#row-img_2').mouseout(function (){ alert('foo2'); });
$('#row-img_1').bind('mouseenter', function(event){
    // event handler for mouseenter
});

$('#row-img_1').bind('mouseleave', function(event){
    // event handler for mouseleave
});
另外,请注意
onmouseover
中缺少的
on

$('#row-img_1').bind('mouseenter', function(event){
    // event handler for mouseenter
});

$('#row-img_1').bind('mouseleave', function(event){
    // event handler for mouseleave
});
或者使用jQuerys hover事件,该事件有效地执行相同的操作

 $('#row-img_1').hover(function(){
    // event handler for mouseenter
 }, function(){
    // event handler for mouseleave

 });
或者使用jQuerys hover事件,该事件有效地执行相同的操作

 $('#row-img_1').hover(function(){
    // event handler for mouseenter
 }, function(){
    // event handler for mouseleave

 });

您需要将事件函数绑定到元素。设置事件属性没有效果,因为只有在加载页面时才会对其进行解释。因此,您需要以不同的方式连接事件回调:

$('#row-img_1').mouseover(function() {
    alert('foo');
});
$('#row-img_2').mouseout(function() {
    alert('foo2');
});
在jQuery中,还有两个事件:和。这些类似,但将鼠标从子元素移动到主元素时,
mouseenter
不会触发,而
mouseover
将再次触发事件。同样的逻辑适用于
mouseleave
vs
mouseout


但是,jquery为这种用法提供了一种快捷方式:方法。

您需要将事件函数绑定到元素。设置事件属性没有效果,因为只有在加载页面时才会对其进行解释。因此,您需要以不同的方式连接事件回调:

$('#row-img_1').mouseover(function() {
    alert('foo');
});
$('#row-img_2').mouseout(function() {
    alert('foo2');
});
在jQuery中,还有两个事件:和。这些类似,但将鼠标从子元素移动到主元素时,
mouseenter
不会触发,而
mouseover
将再次触发事件。同样的逻辑适用于
mouseleave
vs
mouseout


然而,jquery为这种用法提供了一种快捷方式:方法。

+1-值得注意的是,这不是等价的,而是类似于。它映射到
mouseenter
mouseleave
,而不是
mouseover
mouseout
。这很好。通常情况下,
mouseenter
mouseleave
是这些应用程序所需要的东西。@Nick-知道这些很有用+1-值得注意的是,这不是等效的,而是类似的。它映射到
mouseenter
mouseleave
,而不是
mouseover
mouseout
。这很好。通常情况下,
mouseenter
mouseleave
是这些应用程序所需要的东西。@Nick-知道这些很有用!悬停对我有用!我如何解开悬停的带子?我尝试了
$('row-img_1')。解除绑定('hover')但它不起作用。多亏Nicks的评论,我解除了mouseenter和mouseleave的绑定,而不是通常的绑定。我当然可以从你的回答中得出这个结论。谢谢jAndy。@Abs-
。解除绑定('mouseenter mouseleave')
:)悬停对我有用!我如何解开悬停的带子?我尝试了
$('row-img_1')。解除绑定('hover')但它不起作用。多亏Nicks的评论,我解除了mouseenter和mouseleave的绑定,而不是通常的绑定。我当然可以从你的回答中得出这个结论。谢谢jAndy。@Abs-
.unbind('mouseenter mouseleave')
:)