有多个具有相同类的div。我想使用其类在悬停时隐藏/显示每个div。我更喜欢使用mouseenter / mouseleave。但是问题是,$target = this.id;
这似乎不起作用。
如何使用课程显示/隐藏DIV?
$(".zz")
.on( 'mouseenter', function() {
$target = this.id;
if( $target.is(':visible') ) return;
$target.hide();
})
.on( 'mouseleave', function() {
$target = this.id
if( !$target.is(':visible') ) return;
$target.show();
});
编辑:jsFiddle
on()
更好的方法是将所有事件组合为一个方法,而不是将多个方法链接在一起:
$(document).on({
mouseenter: function() {
$(this).find('div[id^="im"]').hide();
},
mouseleave: function() {
$(this).find('div[id^="im"]').show();
},
click: function() {
// do something else
}
}, ".zz");
希望这可以帮助。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句