如何获得标签在html页面,如果我知道什么文本标签包含。 例如:

<a ...>SearchingText</a>

当前回答

function findByTextContent(needle, haystack, precise) { // needle: String, the string to be found within the elements. // haystack: String, a selector to be passed to document.querySelectorAll(), // NodeList, Array - to be iterated over within the function: // precise: Boolean, true - searches for that precise string, surrounded by // word-breaks, // false - searches for the string occurring anywhere var elems; // no haystack we quit here, to avoid having to search // the entire document: if (!haystack) { return false; } // if haystack is a string, we pass it to document.querySelectorAll(), // and turn the results into an Array: else if ('string' == typeof haystack) { elems = [].slice.call(document.querySelectorAll(haystack), 0); } // if haystack has a length property, we convert it to an Array // (if it's already an array, this is pointless, but not harmful): else if (haystack.length) { elems = [].slice.call(haystack, 0); } // work out whether we're looking at innerText (IE), or textContent // (in most other browsers) var textProp = 'textContent' in document ? 'textContent' : 'innerText', // creating a regex depending on whether we want a precise match, or not: reg = precise === true ? new RegExp('\\b' + needle + '\\b') : new RegExp(needle), // iterating over the elems array: found = elems.filter(function(el) { // returning the elements in which the text is, or includes, // the needle to be found: return reg.test(el[textProp]); }); return found.length ? found : false;; } findByTextContent('link', document.querySelectorAll('li'), false).forEach(function(elem) { elem.style.fontSize = '2em'; }); findByTextContent('link3', 'a').forEach(function(elem) { elem.style.color = '#f90'; }); <ul> <li><a href="#">link1</a> </li> <li><a href="#">link2</a> </li> <li><a href="#">link3</a> </li> <li><a href="#">link4</a> </li> <li><a href="#">link5</a> </li> </ul>

当然,还有一种更简单的方法:

var textProp = 'textContent' in document ? 'textContent' : 'innerText'; // directly converting the found 'a' elements into an Array, // then iterating over that array with Array.prototype.forEach(): [].slice.call(document.querySelectorAll('a'), 0).forEach(function(aEl) { // if the text of the aEl Node contains the text 'link1': if (aEl[textProp].indexOf('link1') > -1) { // we update its style: aEl.style.fontSize = '2em'; aEl.style.color = '#f90'; } }); <ul> <li><a href="#">link1</a> </li> <li><a href="#">link2</a> </li> <li><a href="#">link3</a> </li> <li><a href="#">link4</a> </li> <li><a href="#">link5</a> </li> </ul>

引用:

Array.prototype.filter()。 Array.prototype.forEach()。 Array.prototype.slice()。 有条件的('三元',评估?ifTrue: ifFalse)操作符。 Function.prototype.call()。 typeof算子。

其他回答

document.querySelectorAll('a').forEach(function (item) {
    if (item.innerText == 'SearchingText') {
        console.dir(item);
    }
});

你可以这样做,不确定这是否被推荐,但对我来说很有效。

[... document.querySelectorAll('a')].filter(el => el.textContent.includes('sometext'));

你可以使用jQuery:contains()选择器

var element = $( "a:contains('SearchingText')" );

function findByTextContent(needle, haystack, precise) { // needle: String, the string to be found within the elements. // haystack: String, a selector to be passed to document.querySelectorAll(), // NodeList, Array - to be iterated over within the function: // precise: Boolean, true - searches for that precise string, surrounded by // word-breaks, // false - searches for the string occurring anywhere var elems; // no haystack we quit here, to avoid having to search // the entire document: if (!haystack) { return false; } // if haystack is a string, we pass it to document.querySelectorAll(), // and turn the results into an Array: else if ('string' == typeof haystack) { elems = [].slice.call(document.querySelectorAll(haystack), 0); } // if haystack has a length property, we convert it to an Array // (if it's already an array, this is pointless, but not harmful): else if (haystack.length) { elems = [].slice.call(haystack, 0); } // work out whether we're looking at innerText (IE), or textContent // (in most other browsers) var textProp = 'textContent' in document ? 'textContent' : 'innerText', // creating a regex depending on whether we want a precise match, or not: reg = precise === true ? new RegExp('\\b' + needle + '\\b') : new RegExp(needle), // iterating over the elems array: found = elems.filter(function(el) { // returning the elements in which the text is, or includes, // the needle to be found: return reg.test(el[textProp]); }); return found.length ? found : false;; } findByTextContent('link', document.querySelectorAll('li'), false).forEach(function(elem) { elem.style.fontSize = '2em'; }); findByTextContent('link3', 'a').forEach(function(elem) { elem.style.color = '#f90'; }); <ul> <li><a href="#">link1</a> </li> <li><a href="#">link2</a> </li> <li><a href="#">link3</a> </li> <li><a href="#">link4</a> </li> <li><a href="#">link5</a> </li> </ul>

当然,还有一种更简单的方法:

var textProp = 'textContent' in document ? 'textContent' : 'innerText'; // directly converting the found 'a' elements into an Array, // then iterating over that array with Array.prototype.forEach(): [].slice.call(document.querySelectorAll('a'), 0).forEach(function(aEl) { // if the text of the aEl Node contains the text 'link1': if (aEl[textProp].indexOf('link1') > -1) { // we update its style: aEl.style.fontSize = '2em'; aEl.style.color = '#f90'; } }); <ul> <li><a href="#">link1</a> </li> <li><a href="#">link2</a> </li> <li><a href="#">link3</a> </li> <li><a href="#">link4</a> </li> <li><a href="#">link5</a> </li> </ul>

引用:

Array.prototype.filter()。 Array.prototype.forEach()。 Array.prototype.slice()。 有条件的('三元',评估?ifTrue: ifFalse)操作符。 Function.prototype.call()。 typeof算子。

从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))