如何使用 html 文档中的 javascript 动态寻址单词/字符串,然后对其进行标记?
我有一个 HTML 文档:
<html>
<body>
<p>
A funny joke:
<ul>
<li>do you know why six is afraid of seven?
<li>because seven ate nine.
</ul>
Oh, so funny!
</p>
</body>
</html>
现在我想识别第一次出现的“七”并将其标记为
<span id="link1" class="link">
如何完成此操作?
您是否必须解析 DOM 树,或者是否可以获取正文部分中的整个代码,然后搜索该单词?
在这两种情况下,当我在某处找到这个词后,如何识别它并将其 DOM-parent 更改为 span (我猜这就是必须要做的),然后添加提到的属性?
这并不是我所期望的代码,而是什么方法或概念可以完成这项工作。
我对框架解决方案不太感兴趣,而是对纯 JavaScript 方式感兴趣。
I have an HTML-document:
<html>
<body>
<p>
A funny joke:
<ul>
<li>do you know why six is afraid of seven?
<li>because seven ate nine.
</ul>
Oh, so funny!
</p>
</body>
</html>
Now I want to identify the first occurence of "seven" and tag it with
<span id="link1" class="link">
How can this be accomplished?
Do you have to parse the DOM-tree or is it possible to get the whole code within the body-section and then search for the word?
In both cases, after I found the word somewhere, how do you identify it and change it's DOM-parent to span (I guess that's what has to be done) and then add the mentioned attributes?
It's not so much a code I would expect, but what methods or concepts will do the job.
And I am not so much intersted in a framework-solution but in a pure javascript way.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要找到一个类型为 TEXT_NODE (3) 并包含您期望的单词的 DOM 节点。当您需要将该节点拆分为三个节点时。
第一个是 TEXT_NODE,其中包含您搜索的单词之前的文本,第二个是包含您搜索的单词的 SPAN 节点,第三个是另一个包含原始节点尾部的 TEXT_NODE(全部在搜索单词之后)。
这是源代码...
You need to find a DOM node with type TEXT_NODE (3) and containig your expected word. When you need to split a that node into three ones.
First is a TEXT_NODE which contains a text before the word you search, second one is a SPAN node containing the word you search, and third one is another TEXT_NODE containing an original node's tail (all after searched word).
Here is a source code...
这是我几年前编写的一个函数,用于搜索特定文本并突出显示它们(将搜索结果放入具有特定类名的
span
中)。它遍历 DOM 树,检查文本内容。每当它找到包含查找文本的文本节点时,它将用三个新节点替换该文本节点:
span
元素包含匹配文本,这就是我所拥有的功能。它是较大脚本文件的一部分,但它也应该独立运行。 (我注释掉了对
ensureElementVisible
的调用,这使得元素可见,因为脚本还具有折叠和展开功能)。它做了您可能不需要的一件事(其他):它将搜索文本转换为与多个单词中的任何一个匹配的正则表达式。
This is a function I’ve written a few years ago that searches for specific text, and highlights them (puts the hits in a
span
with a specific class name).It walks the DOM tree, examining the text content. Whenever it finds a text node containing the looked-for text, it will replace that text node by three new nodes:
span
element containing the matching text,This is the function as I have it. It’s part of a larger script file, but it should run independently as well. (I’ve commented out a call to
ensureElementVisible
which made the element visible, since the script also had folding and expanding capabilities).It does one (other) thing that you probably won’t need: it turns the search text into a regular expression matching any of the multiple words.
我发现了以下问题:
使用 jQuery 查找文本字符串?
这似乎与您想要的很接近做。现在您是尝试仅换行文本“seven”还是尝试换行
I found the following so question:
Find text string using jQuery?
This appears to be close to what you're trying to do. Now are you attempting to wrap just the text "seven" or are you attempting to wrap the entire content of the <li>?