【问题标题】:How to get a Selector using this如何使用此获取选择器
【发布时间】:2011-12-24 07:36:31
【问题描述】:

我在选择器中有一个选择器。我希望内部选择器只选择那些也由外部选择器选择的元素。 这是我天真的想法:

$('.some-class').hover(function() {
    $( this + '.another-class'); 
})

换句话说,我想要带有another-class AND 的元素,它们是悬停元素的子元素。我该怎么做?

【问题讨论】:

  • 您可以将this 作为第二个参数传递给$。喜欢:$('.another-class', this);

标签: javascript jquery jquery-selectors selector this


【解决方案1】:

使用children() method

$('.some-class').hover(function() {
    $(this).children('.another-class');
});

此方法属于traversal category,它允许您从当前元素中选择祖先、后代、兄弟姐妹等。

【讨论】:

    【解决方案2】:
    $('.some-class').hover(function() {
        $(this).find('.another-class'); 
    })
    

    【讨论】:

      【解决方案3】:

      这个关键字代表对象

      你需要试试这个

      jQuery(".another-class", this);
      

      更多detail

      【讨论】:

      • 需要注意的是,这是在寻找后代,而不仅仅是孩子。
      猜你喜欢
      • 1970-01-01
      • 2010-10-24
      • 1970-01-01
      • 2018-04-11
      • 1970-01-01
      • 1970-01-01
      • 2016-08-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多