jQuery.text()方法時候,會把子元素的文本也獲取到,以下方法可獲取自身文本節點,不包括子元素
<div id="demo">只獲取我<a href="">別管我</a></div> var a = $ ("#demo").prop ('firstChild').nodeValue; console.log(a);
<div id="demo">只獲取我<a href="">別管我</a></div> var a = document.getElementById('demo').firstChild.nodeValue; console.log(a);