开发者

Find text spanning multiple nodes

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜