如何在不使用class或id的情况下选择2nd h2? jQuery的

时间:2018-05-11 19:42:35

标签: jquery jquery-selectors

所以我有一个我需要这个类的脚本,因为我将改变这个类。

有没有办法可以在没有实际使用类的情况下选择.date类(h2)?

<ContextMenu x:Key="RowContextMenu">
    <MenuItem Header="{Binding PlacementTarget.Tag.CurrentLang.CmenuItemUnLockUser, 
                RelativeSource={RelativeSource AncestorType=ContextMenu}}"/>
</ContextMenu>

这是我拥有的东西,但它没有使用第n个选择器。 当我用.date替换它时它起作用。

for...of

有什么想法吗?

2 个答案:

答案 0 :(得分:0)

您可以使用:nth-of-type css选择器。

&#13;
&#13;
console.log(
  document.querySelector('#sortcontainer .gamelink h2:nth-of-type(2)')
);
&#13;
<div id="sortcontainer">
  <li class="game" id="45">
    <a class="gamelink" href="link">
      <img class="image" src="games_category_placeholder.jpg">
      <h2 class="title">Some game</h2>
      <h2 class="date" id="2017">(2017)</h2>
      <h3 class="thumbrating">45/100</h3>
    </a>
  </li>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

jQuery有.eq()方法,用于通过集合中的索引过滤当前元素集合。

$(a).find('h2').eq(1).attr('id') //note that the first element is 0

此外,元素集合可以由jQuery对象的零索引数组引用:

$(a).find('h2')[1].id

在这种情况下,正在使用DOM元素而不是jQuery对象。

.eq()的文档:http://api.jquery.com/eq/

相关问题