如何获得标签在html页面,如果我知道什么文本标签包含。 例如:
<a ...>SearchingText</a>
如何获得标签在html页面,如果我知道什么文本标签包含。 例如:
<a ...>SearchingText</a>
当前回答
document.querySelectorAll('a').forEach(function (item) {
if (item.innerText == 'SearchingText') {
console.dir(item);
}
});
其他回答
使用目前最现代的语法,它可以像这样非常干净地完成:
for (const a of document.querySelectorAll("a")) {
if (a.textContent.includes("your search term")) {
console.log(a.textContent)
}
}
或者使用单独的过滤器:
[...document.querySelectorAll("a")]
.filter(a => a.textContent.includes("your search term"))
.forEach(a => console.log(a.textContent))
当然,遗留浏览器不能处理这个,但是如果需要遗留支持,可以使用转译器。
从user1106925获取filter方法,如果需要,在<=IE11中工作
你可以将展开运算符替换为:
[] .slice.call (document.querySelectorAll(“a”))
和包含调用a.textContent。匹配(“你的搜索词”)
这很简单:
[].slice.call(document.querySelectorAll("a"))
.filter(a => a.textContent.match("your search term"))
.forEach(a => console.log(a.textContent))
这就行了。 返回包含文本的节点数组。
function get_nodes_containing_text(selector, text) {
const elements = [...document.querySelectorAll(selector)];
return elements.filter(
(element) =>
element.childNodes[0]
&& element.childNodes[0].nodeValue
&& RegExp(text, "u").test(element.childNodes[0].nodeValue.trim())
);
}
我只是需要一种方法来获取包含特定文本的元素,这就是我想到的。
使用document.getElementsByInnerText()获取多个元素(多个元素可能具有完全相同的文本),并使用document.getElementByInnerText()获取一个元素(第一次匹配)。
此外,你可以通过使用元素(例如someElement.getElementByInnerText())而不是文档来本地化搜索。
您可能需要调整它以使其跨浏览器或满足您的需求。
我认为代码是不言自明的,所以我将保持原样。
HTMLElement.prototype.getElementsByInnerText = function (text, escape) { var nodes = this.querySelectorAll("*"); var matches = []; for (var i = 0; i < nodes.length; i++) { if (nodes[i].innerText == text) { matches.push(nodes[i]); } } if (escape) { return matches; } var result = []; for (var i = 0; i < matches.length; i++) { var filter = matches[i].getElementsByInnerText(text, true); if (filter.length == 0) { result.push(matches[i]); } } return result; }; document.getElementsByInnerText = HTMLElement.prototype.getElementsByInnerText; HTMLElement.prototype.getElementByInnerText = function (text) { var result = this.getElementsByInnerText(text); if (result.length == 0) return null; return result[0]; } document.getElementByInnerText = HTMLElement.prototype.getElementByInnerText; console.log(document.getElementsByInnerText("Text1")); console.log(document.getElementsByInnerText("Text2")); console.log(document.getElementsByInnerText("Text4")); console.log(document.getElementsByInnerText("Text6")); console.log(document.getElementByInnerText("Text1")); console.log(document.getElementByInnerText("Text2")); console.log(document.getElementByInnerText("Text4")); console.log(document.getElementByInnerText("Text6")); <table> <tr> <td>Text1</td> </tr> <tr> <td>Text2</td> </tr> <tr> <td> <a href="#">Text2</a> </td> </tr> <tr> <td> <a href="#"><span>Text3</span></a> </td> </tr> <tr> <td> <a href="#">Special <span>Text4</span></a> </td> </tr> <tr> <td> Text5 <a href="#">Text6</a> Text7 </td> </tr> </table>
你可以使用TreeWalker遍历DOM节点,并找到所有包含文本的文本节点,并返回它们的父节点:
const findNodeByContent = (text, root = document.body) => { const treeWalker = document.createTreeWalker(root, NodeFilter.SHOW_TEXT); const nodeList = []; while (treeWalker.nextNode()) { const node = treeWalker.currentNode; if (node.nodeType === Node.TEXT_NODE && node.textContent.includes(text)) { nodeList.push(node.parentNode); } }; return nodeList; } const result = findNodeByContent('SearchingText'); console.log(result); <a ...>SearchingText</a>