开发者

Find text spanning multiple nodes

开发者 https://www.devze.com 2023-03-27 10:34 出处:网络
I\'m searching a way to find text spanning multiple nodes in similar way Firefox does it, eg. With given HTML:

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> si开发者_运维知识库t 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.


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号