Javascript nodeValue 返回 null

发布于 2024-09-28 14:31:12 字数 348 浏览 0 评论 0原文

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

<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"/>

问题出在哪里..?

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"/>

wheres the problem..?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不乱于心 2024-10-05 14:31:12

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

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

为了获取文本节点的文本内容:

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

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);
}
千里故人稀 2024-10-05 14:31:12

您缺少第一个Child:(

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)

墨落成白 2024-10-05 14:31:12

IE 7 不支持 节点。

<text> node is not supported in IE 7.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文