无法检查元素是否为hasClass,然后无法为元素设置动画

时间:2013-07-10 22:27:40

标签: javascript jquery

上下文中的解释(WordPress): 我想检查我的li元素是否有一个名为“current-menu-item”的类,如果它我想要它停止animate函数。如果它没有那个类继续动画。此脚本目前无法使用。谢谢你的帮助。

$(document).ready(function ()
{
    $('.nav li a').hover(function() 
    {
        if ($(this).parentNode.hasClass('current-menu-item'))
        {
        alert('this item has the class of current-menu-item');
        }
        else
        {
        $(this).animate({color:'#3b3b3b'}, 300, 'linear');
        }
    }, 
    function() 
        {
        if ($(this).parentNode.hasClass('current-menu-item'))
        {
        // do nothing
        }
        else 
        {
            $(this).animate({color:'#999'}, 300, 'linear');
        }
    });
});

1 个答案:

答案 0 :(得分:3)

if ($(this).parent().hasClass('current-menu-item'))

jQuery对象没有parentNode属性。 DOM元素可以,但parentNode返回的元素没有像.hasClass()这样的jQuery方法。请改用jQuery的.parent() method