私は内部のテキストを含むdivを次のようなテキストの後に置いています:
<div>
text
<span>other text</span>
</div>
私はスパンではないdiv内のテキストだけを取得したい
ありがとう
回答:
回答№1は0あなたは使うことができます 。クローン() そのためには、次のように:
b = $("div").clone();
b.children("span").remove();
alert( b.text() ); // alerts "text"
使用 .clone()
ページに表示される元のDOM要素に影響を与えずに、divのコピーを作成し、スパンを削除してテキストを取得することができます。
回答№2の場合は1
これを試して
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
回答№3の場合は0
var theText = "";
$("div").contents().each(function() {
if(this.nodeType === 3) theText += $(this).text();
});