我对 jquery 子项有一个小问题。
我有这个代码:
<li><a href="#" class="gocinema">cinema</a></li>
<li><a href="#" class="gomusic">music</a></li>
<li><a href="#" class="gogame">game</a></li>
我尝试设置一个函数来更改悬停时链接的颜色。 (我不能简单地使用 css 来做到这一点,因为它用于菜单上的复杂网页设计。)
我尝试做的是创建一个函数:
What is the class of the link i am mousenter ?
If the class is that, do that. If the class is that, do that.
我读了很多关于 jquery 子项的文章,但我不知道如何在函数中表达这一点。
抱歉,如果不是很清楚:/ 谢谢
托马斯
最佳答案
$(document).on('mouseenter mouseleave', 'a', function () {
if ( this.className === 'gocinema' ) {
// do this
}
if ( this.className === 'gomusic' ) {
// do that
}
});
更新
如果您的链接上有多个类,您可以尝试 $.fn.hasClass
:
$(document).on('mouseenter mouseleave', 'a', function () {
var $this = $(this);
if ( $this.hasClass('gocinema') ) {
// do this
}
if ( $this.hasClass('gomusic') ) {
// do that
}
});
关于javascript - 了解函数中 mouseenter 链接的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19361468/