Home > Web Front-end > JS Tutorial > body text

jQuery method to get node and sub-node text_jquery

WBOY
Release: 2016-05-16 16:41:22
Original
1637 people have browsed it

For the html snippet below,

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

Get node plain text:

var text = $('#text_test').text()
Copy after login

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();
Copy after login

Get the text of a child node:

var text = $("#text_test > a").first().contents().filter(function() {
return this.nodeType === 3;
}).text();
Copy after login
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!