简体   繁体   English

我如何找到“td:nth-​​child”号码

[英]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.

相关问题 在中查找文字 <td> jQuery中的nth-child - Find text in <td> nth-child in jquery 如何根据tr:nth-​​child(7)&gt; td:nth-​​child(2)的内容隐藏特定的tr:nth-​​child(6) - How to hide the specific tr:nth-child(6) based on the content of a tr:nth-child(7) > td:nth-child(2) 如何将变量与 nth-child 选择器一起使用? - How can I use variables with nth-child selector? 如何获得 td:nth-child 的空长度? - How to get empty length of td:nth-child? 为什么我可以选择表格中的第n个子单元格,但不能选择第n个子单元格? - Why can I select an nth-child cell, but not an nth-child row in my table? jQuery:td:nth-​​child不能从document.ready中调用 - jQuery: td:nth-child can't be called from document.ready jQuery无法追加 <img> 到特定的表格列/ <td> 具有特定的nth-child()或eq() - jquery can't append <img> to particular table column / <td> with particular nth-child() or eq() 我如何从最后一个元素而不是第一个元素开始使用:nth-​​child()选择器? - How can I use the :nth-child() selector starting from the last element, rather than the first? 我如何使用mootools获得哪个nth-child事件点击 - how can i get which nth-child event click with mootools Jquery使用$(&#39;#mytable tbody tr:nth-​​child()td:nth-​​child()&#39;)选择一个单元格 - Jquery selecting a cell using $('#mytable tbody tr:nth-child() td:nth-child()')
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM