【问题标题】:jQuery .hover() and .click() not workingjQuery .hover() 和 .click() 不起作用
【发布时间】:2012-11-08 16:32:35
【问题描述】:

我正在尝试向我的页面的一部分添加一个简单的悬停和单击功能,但它不起作用。我已经这样做了数百次,没有问题,但这次不同,我不知道为什么?

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;
    });
});

【问题讨论】:

  • this的选择器不应该用引号$(this),而不是$('this')

标签: jquery html click hover


【解决方案1】:
$('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

【讨论】:

  • 我应该看过的!非常感谢
【解决方案2】:

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

【讨论】:

    【解决方案3】:

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

    #wrapper:hover {
        background: red;
    }
    

    【讨论】:

      猜你喜欢
      • 2010-10-04
      • 1970-01-01
      • 2016-08-12
      • 1970-01-01
      • 2015-06-17
      • 2014-12-27
      • 1970-01-01
      • 2011-08-15
      • 1970-01-01
      相关资源
      最近更新 更多