Javascript Javascript搜索标签并获取其innerHTML

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1981417/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-22 22:13:37  来源:igfitidea点击:

Javascript search for tag and get it's innerHTML

javascriptsearchtagsmatchinnerhtml

提问by David R.

It's probably something really simple, but I'm just learning.

这可能是非常简单的事情,但我只是在学习。

There's a page with 3 blockquote tags on it, and I'd need to get the innerHTML of the one containing a certain string. I don't know how to search/match a string and get the innerHTML of the tag containing the matched result.

有一个页面,上面有 3 个 blockquote 标签,我需要获取包含某个字符串的那个页面的 innerHTML。我不知道如何搜索/匹配字符串并获取包含匹配结果的标签的 innerHTML。

Any help would be appreciated!

任何帮助,将不胜感激!

回答by watain

var searchString = 'The stuff in innerHTML';
var elements = document.getElementsByTagName('blockquote')
for (var i = 0; i < elements.length; i++) {
     if (elements[i].innerHTML.indexOf(searchString) !== -1) {
         alert('Match');
         break;
     }
}

:)

:)

Btw there would be a much nicer method if you'd be using Prorotype JS(which is much better than jQuery btw):

顺便说一句,如果您使用Prorotype JS(这比 jQuery 好得多),会有更好的方法:

var el = $$('blockquote').find(function(el) {
    return el.innerHTML.indexOf('The string you are looking for.') !== -1;
});

You could of course also use regular expressions to find the string, which might be more useful (use el.match() for that).

您当然也可以使用正则表达式来查找字符串,这可能更有用(为此使用 el.match())。

回答by Kevin Conner

If you need to search through every <blockquote>on the page, try this:

如果您需要搜索<blockquote>页面上的所有内容,请尝试以下操作:

function findBlockquoteContainingHtml(matchString) {
    var blockquoteElements = document.getElementsByTagName('BLOCKQUOTE');
    var i;
    for (i = 0; i < blockquoteElements.length; i++) {
        if (blockquoteElements[i].innerHTML.indexOf(matchString) >= 0) {
            return blockquoteElements[i].innerHTML;
        }
    }
    return null;
}

回答by Darrell Brogdon

Assign an id to the blockquote elements then you can get the innerHTML like this:

为 blockquote 元素分配一个 id 然后你可以像这样获得innerHTML:

HTML:

HTML:

<blockquote id="bq1">Foo</blockquote>

JS:

JS:

var quote1 = document.getElementById('bq1').innerHTML;

回答by Brian Campbell

Be careful using innerHTMLto search for text within a tag, as that may also search for text in attributes or tags as well.

innerHTML在标签内搜索文本时要小心,因为这也可能在属性或标签中搜索文本。

You can find all blockquoteelements using:

您可以blockquote使用以下方法找到所有元素:

var elems = document.getElementsByTagName("blockquote")

You can then look through their innerHTML, but I would recommend instead looking through their textContent/innerText(sadly, this is not standardized across browser, it seems):

然后你可以查看他们的innerHTML,但我建议你查看他们的textContent/ innerText(遗憾的是,这似乎不是跨浏览器的标准化):

for (i in elems) {
  var text = elems[i].textContent || elems[i].innerText;
  if (text.match(/foo/)) {
    alert(elems[i].innerHTML);
  }
}