【发布时间】:2013-06-22 11:48:57
【问题描述】:
我希望在悬停时交换一个 img src。通常我会使用:
$('#img').hover(function() {
$(this).attr('src', 'http://www.example.com/new-img.jpg');
});
但是,我通过 Ajax 加载内容,所以通常我会使用:
$('#main').on('hover', '#img', function() {
$('#img').attr('src', 'http://www.example.com/new-img.jpg');
});
但我读到 on('hover', ...) 在 jQuery 1.8 中已弃用并在 1.9 (jQuery Docs) 中删除,这是我目前使用的。除了使用:
$('#main').on('mouseenter', '#img', function() {
$('#img').attr('src', 'http://www.example.com/new-img.jpg');
});
$('#main').on('mouseleave', '#img', function() {
$('#img').attr('src', 'http://www.example.com/old-img.jpg');
});
【问题讨论】:
-
使用mouseenter和mouseleave有什么问题?
-
没问题,我只是想看看是否有人有更短/更简单的解决方法。
标签: jquery jquery-hover jquery-on