【问题标题】:jQuery "this" plus other selectors?jQuery“this”加上其他选择器?
【发布时间】:2012-01-30 17:20:05
【问题描述】:

是否可以将$(this)$('.someclass') 结合使用?

例如:$(this + ', .someclass').hover();

我正在尝试使用悬停动作来为非后代类设置动画。我希望类元素动画“它”,即 $('.someclass') 或 $(this) 是否悬停。

我希望这可以在不创建 2 个单独的悬停动作的情况下完成。

【问题讨论】:

  • 如果this 有一个id$('#' + this.id + ', .someclass').hover();

标签: jquery hover


【解决方案1】:

你可以使用jQuery的.add()

$(this).add('.someClass').hover();

【讨论】:

    【解决方案2】:

    您可以使用选择器上下文:http://api.jquery.com/jQuery/#jQuery-selector-context

    $('+ .someclass', this).hover();
    

    这是一个示例:将鼠标悬停在第一个兄弟姐妹上,第二个兄弟姐妹的背景颜色将被更改。

    $('.sibling1').hover(
      function() {
        $('+ .sibling2', this).css({"background":"#ff0"});
      },
      function() {
        $('+ .sibling2', this).css({"background":"#eee"});
      }
    );
    div {
      padding:5px;
      background:#eee;
      margin-bottom:5px;
      display:inline-block;
      cursor:pointer;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    
    <div class="sibling1">Sibling 1</div>
    <div class="sibling2">Sibling 2</div>

    【讨论】:

      【解决方案3】:

      另一种选择是使用 jQuery find()

      $(this).find('.someclass').hover();
      

      【讨论】:

      • 这将寻找.someclass 作为this 的子级。它不等同于选择器中的,
      猜你喜欢
      • 2011-07-19
      • 2010-10-24
      • 1970-01-01
      • 1970-01-01
      • 2011-07-26
      • 2011-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多