Javascript 搜索标签并获取它的innerHTML
这可能很简单,但我只是在学习。
有一个页面上有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
:)
顺便说一句,如果你使用 Prorotype JS (顺便说一句,这比 jQuery 好得多),会有一个更好的方法):
您当然也可以使用正则表达式来查找字符串,这可能更有用(使用 el.match() )。
:)
Btw there would be a much nicer method if you'd be using Prorotype JS (which is much better than jQuery btw):
You could of course also use regular expressions to find the string, which might be more useful (use el.match() for that).
如果您需要搜索页面上的每个
If you need to search through every
<blockquote>
on the page, try this:为 blockquote 元素分配一个 id,然后你可以得到如下的 insideHTML:
HTML:
JS:
Assign an id to the blockquote elements then you can get the innerHTML like this:
HTML:
JS:
使用
innerHTML
搜索标记内的文本时要小心,因为它也可能搜索属性或标记中的文本。您可以使用以下方法找到所有
blockquote
元素:然后您可以查看它们的
innerHTML
,但我建议您查看它们的textContent
/innerText
(遗憾的是,这似乎在浏览器中没有标准化):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:You can then look through their
innerHTML
, but I would recommend instead looking through theirtextContent
/innerText
(sadly, this is not standardized across browser, it seems):