javascript 使用 querySelector() 获取最后一个 td 元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29398924/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 10:28:59  来源:igfitidea点击:

Using querySelector() to obtain last td element

javascriptselectors-api

提问by Doug Fir

I have a variable which is a node from the dom. I've managed to get all the way down to close to where I want to be:

我有一个变量,它是来自 dom 的一个节点。我已经设法一路接近我想要的地方:

myvar.querySelector('.tblItinPriceSummary tr')

Gives me this:

给我这个:

<tr>
    <td>Subtotal</td>
    <td align="right">9.00</td>
</tr>

What I want is the textContent of the second td $189.

我想要的是第二个 td 189 美元的 textContent。

Is there anything I can add inside of querySelector so that I can append it with .textContent to get this piece of data?

有什么我可以在 querySelector 中添加的东西,以便我可以用 .textContent 附加它来获取这段数据?

回答by Josh Crozier

You could either use :last-childor :last-of-typeto access the last tdelement within the parent.

您可以使用:last-child:last-of-type访问td父级中的最后一个元素。

document.querySelector('.tblItinPriceSummary tr td:last-child').textContent;