可能重複:
jquery - get text for element without children text獲取文本在DIV
我曾與文本內跨越一個div文字像這樣經過:
<div>
text
<span>other text</span>
</div>
和我d只想得到div中的文本而不是跨度
感謝
可能重複:
jquery - get text for element without children text獲取文本在DIV
我曾與文本內跨越一個div文字像這樣經過:
<div>
text
<span>other text</span>
</div>
和我d只想得到div中的文本而不是跨度
感謝
您可以使用.clone()的是,像這樣:
b = $('div').clone();
b.children('span').remove();
alert(b.text()); // alerts "text"
使用.clone()
我們可以使DIV的拷貝,刪除時長,然後再拿到短信,都不會影響原始DOM元素顯示在頁面上。
var theText = "";
$('div').contents().each(function() {
if(this.nodeType === 3) theText += $(this).text();
});
該方法是['.contents()'](http://api.jquery.com/contents)。否則,這個工程! – nbrooks
試試這個
var myText = $('#dd').clone()
.children() //select all the children
.remove() //remove all the children
.end() //again go back to selected element
.text(); //get the text of element
Upvoted,我喜歡你的.end()快捷方式。 – Nelson
我會很短約這樣的:我不明白這一點。 –