Javascript nodeValue返回null [英] Javascript nodeValue returns null

查看:136
本文介绍了Javascript nodeValue返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

标题应该让我的问题得到很好的描述。这是我的代码。

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="vb();" value="get"/>

问题是什么??

推荐答案

为了获得元素节点的[合并]文本内容:

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);
}

这篇关于Javascript nodeValue返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆