开发者

Javascript nodeValue returns null

开发者 https://www.devze.com 2023-01-21 02:50 出处:网络
Title should make my problem well described.Here goes my code. <div id=\"adiv\"><text>Some text</text></div>

Title should make my problem well described.Here goes my code.

<div id="adiv"><text>Some text</text></div>    
<script type="text/javascript">
function vb(){
alert(document.getElementById("adiv").firstChild.nodeValue); //returns null
}
</script>
<input type="button" onclick="v开发者_开发技巧b();" value="get"/>

wheres the problem..?


In order to get [merged] text content of an element node:

function vb(){
var textnode = document.getElementById("adiv").firstChild;
alert(textnode.textContent || textnode.innerText);
}

In order to get text content of a text node:

function vb(){
alert(document.getElementById("adiv").firstChild.firstChild.nodeValue);
}


You are missing a firstChild:

alert(document.getElementById("adiv").firstChild.firstChild.nodeValue);

(I know it sounds weird but this is how text nodes work)


<text> node is not supported in IE 7.

0

精彩评论

暂无评论...
验证码 换一张
取 消