查找跨越多个节点的文本

发布于 2024-11-29 15:20:03 字数 383 浏览 0 评论 0原文

我正在寻找一种方法来查找跨越多个节点的文本,其方式与 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 技术交流群。

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

发布评论

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

评论(2

仙女 2024-12-06 15:20:03

这将选择包含指定为 indexOf 参数的文本的所有 p 元素。 text 方法获取元素的所有文本节点的内容,因此示例中的 b 标记并不重要:

$("p").filter(function() {
  return $(this).text().indexOf("ipsum dolor") > -1;  
});

查看它的工作情况 此处

This will select all p elements that contain the text specified as an argument to indexOf. The text method gets the contents of all text nodes of an element, so the b tag in your example will not matter:

$("p").filter(function() {
  return $(this).text().indexOf("ipsum dolor") > -1;  
});

See it working here.

昔梦 2024-12-06 15:20:03

window.find 正是我正在寻找的为了。

window.find is exactly what I'm looking for.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文