jquery添加onmouseover属性



在jquery中,如何将'onmouseover'事件添加到元素中。

例如

<tr id=row bgcolor=white>

成为

 <tr id=row bgcolor=white onMouseOver="this.bgColor='red'">

您可以使用attr方法:

$('#row').attr("onMouseOver", "this.bgColor='red'")

但由于您使用的是jQuery,我建议您使用on方法:

$('#row').on('mouseover', function() {
    $(this).css('background-color', 'red');
});

如果元素是静态的,请尝试此操作:

var $row = $('#row');
$row.mouseover(function(){
    $row.css('background-color','red');
});

如果元素是动态放置在页面中,则使用此选项:

var $row = $('#row');
$row.on('mouseover',function(){
    $row.css('background-color','red');
});

不要添加属性。使用事件。

$('#row').mouseover(function() {
  $(this).css('background','red');
});

最新更新