Home  >  Article  >  Web Front-end  >  jQuery method to get node and sub-node text_jquery

jQuery method to get node and sub-node text_jquery

WBOY
WBOYOriginal
2016-05-16 16:41:221635browse

For the html snippet below,

<div id="text_test">test text<a href="techbrood.com" rel="external nofollow" >techbrood co.</a></div>

Get node plain text:

var text = $('#text_test').text()

This will get "test text techbrood co.", which means the text of all nodes (including child nodes) of the current element will be read out.

If you only want to get the text of the main node, the method is more complicated:

var text = $("#text_test").contents().filter(function() {
return this.nodeType === 3;
}).text();

Get the text of a child node:

var text = $("#text_test > a").first().contents().filter(function() {
return this.nodeType === 3;
}).text();
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn