jQuery .hover()和.click()不起作用

时间:2012-11-08 16:32:35

标签: jquery html click hover

我正在尝试添加一个简单的悬停和点击功能到我的页面的一部分,它不起作用。我已经完成了这一百次没有问题,但这次是不同的,我找不到原因?

HTML:

<div class="description">
    <div id="wrapper">
        <p>bla bla bla</p>
        <div class="linkDesc">
            <a href="#">bla bla bla</a>
        </div>
        <div class="plusSign">&nbsp;</div>
    </div>
</div>

jQuery的:

jQuery(document).ready(function () {

    $('#wrapper').hover(function () {
        $('this').addClass('hover');
    }, function () {
        $('this').removeClass('hover');
    });
    $('#wrapper').click(function(e) {
        e.preventDefault();
        var urlLocation = $('this').find('.linkDesc').children('a').attr('href');
        window.location = urlLocation;
    });
});

3 个答案:

答案 0 :(得分:7)

$('this')   // You have enclosed it as as a string
            // It will try to look for tagName called this

应该是

$(this)    // Removing the Quotes should work

只有合法TagNames , classNames , psuedo Selectors and Id's应该包含在引号 ..

<强> Check Fiddle

答案 1 :(得分:0)

$('this')替换为$(this)

答案 2 :(得分:0)

如果您只需要添加/删除类,则可以使用css设置悬停:

#wrapper:hover {
    background: red;
}
相关问题