使用Javascript选择嵌套元素

时间:2018-07-02 01:25:03

标签: javascript

如何选择嵌套元素

例如

我们有一个类似的东西(这是在 linkedin 上搜索人员时页面列表的外观)

<li class="page-list">
      <ol>
            <li class="active">1</li>
            <li><button data-ember-action="" data-ember-action-6289="6289" data-is-animating-click="true">2</button></li>
            <li><button data-ember-action="" data-ember-action-6291="6291">3</button></li>
            <li><button data-ember-action="" data-ember-action-6293="6293">4</button></li>
            <li><button data-ember-action="" data-ember-action-6295="6295">5</button></li>
            <li><button data-ember-action="" data-ember-action-6297="6297">6</button></li>
            <li><button data-ember-action="" data-ember-action-6299="6299">7</button></li>
            <li><button data-ember-action="" data-ember-action-6301="6301">8</button></li>
            <li><button data-ember-action="" data-ember-action-6303="6303">9</button></li>
            <li><button data-ember-action="" data-ember-action-6305="6305">10</button></li>
      </ol>
    </li>

现在,就像这样,页面上有多个li元素,但是只有一个元素为<li class="page-list">

现在,我要从此li单击/选择此

<button data-ember-action="" data-ember-action-6291="6291">3</button>

进一步嵌套在<li>

[问题] 有人可以帮助我,告诉我如何选择/单击它吗?另外,我无法使用data-ember-action,因为在data-ember-action-6289中,6289不是固定且随机的(可能因不同的搜索而异),因此当我创建脚本来执行操作时,它不会了解data-ember-action中的数字。

1 个答案:

答案 0 :(得分:2)

一个简单的querySelector可以做到:

console.log(document.querySelector('[data-ember-action-6291="6291"]'))
<li class="page-list">
  <ol>
    <li class="active">1</li>
    <li><button data-ember-action="" data-ember-action-6289="6289" data-is-animating-click="true">2</button></li>
    <li><button data-ember-action="" data-ember-action-6291="6291">3</button></li>
    <li><button data-ember-action="" data-ember-action-6293="6293">4</button></li>
    <li><button data-ember-action="" data-ember-action-6295="6295">5</button></li>
    <li><button data-ember-action="" data-ember-action-6297="6297">6</button></li>
    <li><button data-ember-action="" data-ember-action-6299="6299">7</button></li>
    <li><button data-ember-action="" data-ember-action-6301="6301">8</button></li>
    <li><button data-ember-action="" data-ember-action-6303="6303">9</button></li>
    <li><button data-ember-action="" data-ember-action-6305="6305">10</button></li>
  </ol>
</li>

如果您不能指望data-属性,那么只需选择ol的第三个孩子:

console.log(
  document.querySelector('.page-list li:nth-child(3)')
);
<li class="page-list">
  <ol>
    <li class="active">1</li>
    <li><button data-ember-action="" data-ember-action-6289="6289" data-is-animating-click="true">2</button></li>
    <li><button data-ember-action="" data-ember-action-6291="6291">3</button></li>
    <li><button data-ember-action="" data-ember-action-6293="6293">4</button></li>
    <li><button data-ember-action="" data-ember-action-6295="6295">5</button></li>
    <li><button data-ember-action="" data-ember-action-6297="6297">6</button></li>
    <li><button data-ember-action="" data-ember-action-6299="6299">7</button></li>
    <li><button data-ember-action="" data-ember-action-6301="6301">8</button></li>
    <li><button data-ember-action="" data-ember-action-6303="6303">9</button></li>
    <li><button data-ember-action="" data-ember-action-6305="6305">10</button></li>
  </ol>
</li>