document.querySelectorAll立即获取所有选定元素的innerText纯javascript [英] document.querySelectorAll get innerText of ALL selected elements at once pure javascript

查看:481
本文介绍了document.querySelectorAll立即获取所有选定元素的innerText纯javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获取一个非常长的html表(随机长度)的整个列的所有innerText. 我正在使用此代码:

I want to get all innerText of a whole column of a very long html table (random length). I'm using this code:

var tbEls = document.querySelectorAll('#tBodyID tr td:nth-child(cidx)');

cidx =我要从中提取内容的列索引.

Where cidx = the column index I want to extract content from.

但是这样的代码提取了所有的td元素(当然,其中包含了innerText). 但是它不会直接提取其中的所有innerText.由于这个原因,我必须使用for循环重新处理返回的tdEls数组,以从每个tbEls [i]元素中提取其自己的innerText.可以,但是...

But such code extracts all the td elements (with the innerText inside them of course). But it doesn't extract directly all the innerText inside them. Cause of this I have to reprocess the returned tdEls array with a for loop to extract from each tbEls[i] element its own innerText. It works but...

我的问题是:

在纯JS(无外部库或框架)中,可以使用更直接的方法进行某种方式的改进,仅此而已,将querySelectorAll参数('#tBodyID tr td:nth-child(cidx)')直接获取所有td一次在一个JavaScript语句中添加元素innerText ,而无需使用for循环或其他方法重新处理返回的数组?

In pure JS (no external libraries or frameworks) is it possible to use a more direct approach improving some way just and only the querySelectorAll parameter ('#tBodyID tr td:nth-child(cidx)') to get directly all the td elements innerText at once and in just one javascript statement and without the need of reprocessing the returned array with the for loop or anything else?

换句话说,是否有某种innerText选择器可用于一次获取所有内容而没有任何额外的循环?

In other words is there a some kind of innerText selector that can be used to get them all at once without any kind of extra loop?

如果旧的浏览器无法识别,则完全没有问题,对不起.

No problem at all if it is not recognized by old browsers, I'm sorry for them.

我希望实现的目标是:

var arrTblColInnerText = document.querySelectorAll('#tBodyID tr td:nth-child(cidx):alltd:innerText');

我想要一个类似于以下内容的数组:

I want to get an array similar to:

0: value from column cidx cell 0
1: value from column cidx cell 1
2: value from column cidx cell 2
3: value from column cidx cell 3
...
n: value from column cidx cell n

谢谢.

推荐答案

我发现的最简单的方法是先将nodeList转换为数组,然后使用映射:

The easiest way I found was to convert the nodeList to an array first then use a map:

var nodes = document.querySelectorAll("h3 em");
var list = [].slice.call(nodes);
var innertext = list.map(function(e) { return e.innerText; }).join("\n");

这篇关于document.querySelectorAll立即获取所有选定元素的innerText纯javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆