【发布时间】:2021-12-10 18:40:16
【问题描述】:
我有多个具有相同类名的元素,我想选择最后一个。 我尝试了以下方法,但它们不起作用。 任何帮助将不胜感激。
const elem = $('.example')[-1]
const elem = $('.example[-1]')
const elem = $('.example').last()
【问题讨论】:
标签: webdriver-io
我有多个具有相同类名的元素,我想选择最后一个。 我尝试了以下方法,但它们不起作用。 任何帮助将不胜感激。
const elem = $('.example')[-1]
const elem = $('.example[-1]')
const elem = $('.example').last()
【问题讨论】:
标签: webdriver-io
您可以使用 ":last-child" CSS 伪选择器:
const elem = $('.example:last-child')
https://developer.mozilla.org/en-US/docs/Web/CSS/:last-child
【讨论】:
:last-of-type 也可能有用:)
例如我们得到这样一篇文章:
<article>
<h1>A Title</h1>
<p>Paragraph 1.</p>
<p>Paragraph 2.</p>
<p>Paragraph 3.</p>
<img src="...">
</article>
我们可以像这样选择最后一个 p 元素:
const elem = $('p:last-of-type')
【讨论】: