查找跨越多个节点的文本
我正在寻找一种方法来查找跨越多个节点的文本,其方式与 Firefox 类似,例如。
对于给定的 HTML:
<p>Lorem ipsum <b>dolor</b> sit amet.</p>
当我通过 ctrl+f 搜索文本“ipsum dolor”时,Firefox 将选择该文本,即。将创建 Range 对象。
我知道我可以轻松地在文本节点中搜索文本(参见 使用 jQuery 查找文本字符串?) 但这在上面的例子中不起作用。
I'm searching a way to find text spanning multiple nodes in similar way Firefox does it, eg.
With given HTML:
<p>Lorem ipsum <b>dolor</b> sit amet.</p>
When I search for text "ipsum dolor" by ctrl+f Firefox will selects that text, ie. will create Range object(s).
I know I can easily search for text within text nodes (vide Find text string using jQuery?) but this doesn't work in above example.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这将选择包含指定为
indexOf
参数的文本的所有p
元素。text
方法获取元素的所有文本节点的内容,因此示例中的b
标记并不重要:查看它的工作情况 此处。
This will select all
p
elements that contain the text specified as an argument toindexOf
. Thetext
method gets the contents of all text nodes of an element, so theb
tag in your example will not matter:See it working here.
window.find
正是我正在寻找的为了。window.find
is exactly what I'm looking for.