【问题标题】:Why isn't my jQuery script working?为什么我的 jQuery 脚本不起作用?
【发布时间】:2015-10-14 00:43:44
【问题描述】:
$(document).ready(function(){

  $('.footer-item').hover(function(){

    $(this).addClass(active);
    $('.footer-item active').removeClass('active');

});

});

基本上我希望这个脚本做的是更改活动图标上的类,使其看起来更透明,并且通过添加一个类使悬停在上面的图标更加透明。

【问题讨论】:

  • $(this).addClass("active");
  • 还有$('.footer-item .active')
  • 谢谢大家,它现在可以正常工作了。 :)

标签: jquery tabs footer opacity


【解决方案1】:

您的代码有几个问题。

  • 您需要先删除活动类,否则您将添加活动类,然后立即删除它(本质上是“什么都不做”。)
  • addClass 中的'active' 应该是一个字符串。
  • 具有页脚项和活动类的元素的正确选择器是'.footer-item.active'

    $(document).ready(function(){
    
      $('.footer-item').hover(function(){
    
        $('.footer-item.active').removeClass('active');
        $(this).addClass('active');
    
       });
    
    });
    

$(document).ready(function(){

  $('.footer-item').hover(function(){

    $('.footer-item.active').removeClass('active');
    $(this).addClass('active');

});

});
.active { background: red; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="footer-item">Item 1</div>
<div class="footer-item">Item 2</div>
<div class="footer-item">Item 3</div>
<div class="footer-item">Item 4</div>

【讨论】:

    猜你喜欢
    • 2013-11-18
    • 2013-02-02
    • 1970-01-01
    • 2016-11-03
    • 2012-10-07
    • 1970-01-01
    • 1970-01-01
    • 2017-05-30
    相关资源
    最近更新 更多