How to get the text node of an element?
P粉518799557
P粉518799557 2023-08-24 10:28:37
0
2
583
<p><pre class="brush:php;toolbar:false;"><div class="title"> I am text node <a class="edit">Edit</a> </div></pre> <p>I want to get the "I am text node", don't want the "edit" tag to be removed, and need a cross-browser solution. </p>
P粉518799557
P粉518799557

reply all(2)
P粉148434742

You can use the following method to get the nodeValue of the first child node

$('.title')[0].childNodes[0].nodeValue

http://jsfiddle.net/TU4FB/

P粉239164234
var text = $(".title").contents().filter(function() {
  return this.nodeType == Node.TEXT_NODE;
}).text();

This takes the content of the selected element and applies the filter function to it. The filter function returns only text nodes (i.e. those with nodeType == Node.TEXT_NODE).

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!