![](/img/trans.png)
[英]Javascript window.atob -> HEX - different result than expected
[英]javascript function return different result than expected
在這個網站
https://www.worldometers.info/coronavirus/
我用這個javascript function 知道表中國家的position
{
function findMatchingRow(word) {
const found = []
const trList = document.querySelectorAll('#main_table_countries_today > tbody > tr')
trList.forEach((tr, i) => {
if (tr.textContent.match(word)) {
found.push({
index: i,
content: tr.textContent
})
}
});
return found
}
const matches = findMatchingRow("Australia")
console.log(matches)
if (matches.length > 0) {
console.log('found at:', matches.map(m => m.index))
}
}
僅對於澳大利亞,它返回 8 而不是 35
對於像波蘭這樣的其他國家,它給出了正確的數字,
我還是想不通
任何幫助將不勝感激 !
您不必從整行中獲取文本內容,您只需匹配第一個td
內容即可。 tr
的任何地方都有像澳大利亞這樣的地方。 所以縮小搜索范圍。
function findMatchingRow(word) {
const trList = [...document.querySelectorAll(
"#main_table_countries_today > tbody > tr"
)];
let found;
trList.some((tr, i) => {
const name = tr.children[0].textContent.trim();
if (name.includes(word)) {
found = {
index: i,
content: tr.textContent,
};
}
return found;
});
return found;
}
const found = findMatchingRow("Australia");
if (found) {
console.log("found at:"+ JSON.stringify(found));
console.log("found at:"+ found.index);
}
您只搜索整個 tr (行)的文本內容。 text-Content 都是節點。 澳大利亞導致一個數組,因為許多條目將“澳大利亞/大洋洲”作為數據大陸屬性。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.