[英]How to get a HTML element by searching the content of innerHTML?
How to get a HTML element by searching the content of innerHTML? 如何通过搜索innerHTML的内容来获取HTML元素?
For example, Search 例如,搜索
I want to get the "a" tag by searching word "Search" using javascript. 我想通过使用javascript搜索“搜索”字词来获取“a”标签。
Thank you 谢谢
var aTags = document.querySelectorAll('a');
var searchaTag = '';
for(i = 0; i<aTags.length; i++ ) {
if(aTags[i].text == 'Search') {
searchaTag = aTags[i];
break;
}
}
If you have no idea where the element can be, iterate over each element: 如果您不知道元素的位置,请迭代每个元素:
var all = document.querySelectorAll('*'); //OR document.getElementsByTagName("*")
for(var i = 0; i < all.length; i++ ) {
if(all[i].innerHTML === "search") {
console.log(all[i].tagName);
}
}
If you have some clue you can narrow your search by looking inside that area: 如果你有一些线索,你可以通过查看该区域来缩小搜索范围:
var all = document.querySelector('#my-Container').getElementsByTagName('*');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.