开发者

jQuery to select text from HTML

I'm loo开发者_运维百科king to get the HTML of the page search for some text and then select the element it belongs to with jQuery. Unfortunately there are no unique IDs for the elements.


You should be able to do this

var element = $('*:contains("search text")');


Consider this:

<div><span>some text</span></div>

Then, to get only the span when you do the search, do this:

var elements = $('body *').contents().filter(function() {
    return this.nodeType == 3 && $(this).text().indexOf("some text") != -1;
}).parent();

If the text is found elsewhere, elements will contain the other elements as well. For example:

<div><span>some text</span><p>some text</p><strong>not the same text</strong></div>

Results will contain the span and the p elements.

A final example:

<div>some text<span>some text</span><p>some text</p><strong>not the same text</strong></div>

Now results will have the div too, as it is the direct parent of the text node (i.e. some text)

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜