Jquery获取文本节点不获取子元素的文本
记录下,
1.获取节点下所有的元素
<div class="warp_cont">
hellowWord
<span>middle</span>
hellow_char
</div>
//获取了所有的节点元素
<script type="text/javascript">
$(function(){
var txt = $(".warp_cont").text();
console.log(txt);//hellowWord middle hellow_char
});
</script>
2.获取元素子元素前面的文本
<div class="warp_cont">
hellowWord
<span>middle</span>
hellow_char
</div>
<script type="text/javascript">
$(function(){
var txt_1 = $(".warp_cont").prop("firstChild").nodeValue;
var txt_2 = $(".content").contents()[0].nodeValue;
var txt_3 = $(".content").prop("childNodes")[0].nodeValue;
console.log(txt_1);//hellowWord
console.log(txt_2);//hellowWord
console.log(txt_3);//hellowWord
});
</script>
3.获取元素子元素后面的文本
<div class="warp_cont">
hellowWord
<span>middle</span>
hellow_char
</div>
<script type="text/javascript">
$(function(){
var txt_1 = $(".warp_cont").prop("lastChild").nodeValue;
var txt_2 = $(".content").contents()[2].nodeValue;
var txt_3 = $(".content").prop("childNodes")[2].nodeValue;
console.log(txt_1);//hellow_char
console.log(txt_2);//hellow_char
console.log(txt_3);//hellow_char
});
</script>
–end–