對於下面的html片段,
1
|
<
div
id
=
"text_test"
>test text<
a
href
=
"techbrood.com"
rel
=
"external nofollow"
>techbrood co.</
a
></
div
>
|
獲取節點純文本:
1
|
var
text = $(
'#text_test'
).text()
|
這個會得到“test text techbrood co.”,也就是會把當前元素的所有節點(包含子節點)的文本讀取出來。
如果只想獲取主節點的文本,方法復雜點:
1
2
3
|
var
text = $(
"#text_test"
).contents().filter(
function
() {
return
this
.nodeType === 3;
}).text();
|
獲取某子節點的文本:
1
2
3
|
var
text = $(
"#text_test > a"
).first().contents().filter(
function
() {
return
this
.nodeType === 3;
}).text();
|