Javascript 搜索标签并获取它的innerHTML

发布于 2024-08-16 08:53:59 字数 131 浏览 4 评论 0原文

这可能很简单,但我只是在学习。

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

任何帮助将不胜感激!

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.

Any help would be appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

感情废物 2024-08-23 08:53:59
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;
     }
}

:)

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

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

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

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

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

陌生 2024-08-23 08:53:59

如果您需要搜索页面上的每个

,请尝试以下操作:

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;
}

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

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;
}
离不开的别离 2024-08-23 08:53:59

为 blockquote 元素分配一个 id,然后你可以得到如下的 insideHTML:

HTML:

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

JS:

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

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

HTML:

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

JS:

var quote1 = document.getElementById('bq1').innerHTML;
带上头具痛哭 2024-08-23 08:53:59

使用 innerHTML 搜索标记内的文本时要小心,因为它也可能搜索属性或标记中的文本。

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

var elems = document.getElementsByTagName("blockquote")

然后您可以查看它们的 innerHTML,但我建议您查看它们的 textContent/innerText (遗憾的是,这似乎在浏览器中没有标准化):

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

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

You can find all blockquote elements using:

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

for (i in elems) {
  var text = elems[i].textContent || elems[i].innerText;
  if (text.match(/foo/)) {
    alert(elems[i].innerHTML);
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文