[英]How can i find "td:nth-child" number
I have tbody that have a list,我有一个有清单的人,
here's the website: https://www.worldometers.info/coronavirus/这是网站: https : //www.worldometers.info/coronavirus/
the child number change every some hours孩子的号码每隔几个小时就会改变一次
i tried to search for the word that is in tr我试图搜索 tr 中的单词
var word = "Tunisia",
queue = [document.body],
curr
;
while (curr = queue.pop()) {
if (!curr.textContent.match(word)) continue;
for (var i = 0; i < curr.childNodes.length; ++i) {
switch (curr.childNodes[i].nodeType) {
case Node.TEXT_NODE : // 3
if (curr.childNodes[i].textContent.match(word)) {
console.log("Found!");
console.log(curr);
// you might want to end your search here.
}
break;
case Node.ELEMENT_NODE : // 1
queue.push(curr.childNodes[i]);
if (curr.childNodes[i].textContent.match(word)) {
}
break;
}
}
}
but i don't know how to find the child number after i found the line但我不知道在找到这条线后如何找到孩子的号码
for now, it's 81现在是 81
for exemple:举个例子:
document.querySelector("#main_table_countries > tbody:nth-child(2) > tr:nth-child(81) > td:nth-child(3)").innerText
Sorry, for the bad english and explantation !对不起,英语和解释不好!
It seems like you want to know the row number that matches your word?您似乎想知道与您的单词匹配的行号?
This will find each row in that specific table ( #main_table_countries
) and return a list of matches, including the tr
index for each, plus its full text:这将找到该特定表 (
#main_table_countries
) 中的每一行并返回匹配列表,包括每个匹配的tr
索引及其全文:
function findMatchingRow(word) {
const found = []
const trList = document.querySelectorAll('#main_table_countries > tbody > tr')
trList.forEach((tr, i) => {
if(tr.textContent.match(word)) {
found.push({index: i, content: tr.textContent})
}
})
return found
}
const matches = findMatchingRow('Tunisia')
console.log(matches)
if(matches.length > 0) {
console.log('found at:', matches.map(m => m.index))
}
It allows for multiple matches, but you can change that if it's not necessary.它允许多个匹配,但如果没有必要,您可以更改它。 (For example, "Guinea" would return multiple rows, as of now it would give you both "Guinea" and "Equatorial Guinea")
(例如,“Guinea”会返回多行,截至目前它会同时提供“Guinea”和“Equatorial Argentina”)
You can get the position relative to the parent element, try this:您可以获得相对于父元素的位置,试试这个:
var word = "Tunisia",
queue = [document.body],
curr
;
while (curr = queue.pop()) {
if (!curr.textContent.match(word)) continue;
for (var i = 0; i < curr.childNodes.length; ++i) {
switch (curr.childNodes[i].nodeType) {
case Node.TEXT_NODE : // 3
if (curr.childNodes[i].textContent.match(word)) {
console.log("Found!");
console.log(curr);
console.log(curr.offsetLeft, curr.offsetTop); //Here
// you might want to end your search here.
}
break;
case Node.ELEMENT_NODE : // 1
queue.push(curr.childNodes[i]);
if (curr.childNodes[i].textContent.match(word)) {
}
break;
}
}
}
But since you're running through the entire body
it might not exactly the solution but hope it helps you get there.但是,由于您正在运行整个
body
因此它可能不完全是解决方案,但希望它可以帮助您到达那里。
You can use document.querySelectorAll to store all of the rows, then search through the first TD element in each's innerText until you find one that contains "Tunisia", then exit the loop after you perform whatever changes you wish to make.您可以使用 document.querySelectorAll 存储所有行,然后搜索每个 innerText 中的第一个 TD 元素,直到找到包含“Tunisia”的元素,然后在执行您希望进行的任何更改后退出循环。
var trs = document.querySelectorAll("#main_table_countries > tbody:nth-child(2) > tr");
for (var i = 0; i < trs.length; i++){
var tds = trs[i].querySelector("td");
if (tds.innerText.indexOf("Tunisia") != -1){
tds.style.border = "2px red solid"; // as an example
break;
}
}
i
is your row number, in this example, and tds
refers to the element within that row which contains the text.在本例中,
i
是您的行号, tds
是指该行中包含文本的元素。 So to change the entire row's color, you would use trs[i]
因此,要更改整行的颜色,您可以使用
trs[i]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.