首页 > js中nodeValue的作用是什么啊

js中nodeValue的作用是什么啊

<html>
    <head>
        <title></title>
        <meta http-equiv="content-type" content="text/html;charset=utf-8"/>
        <script type="text/javascript">
            window.onload=function(){
                var a=document.getElementsByTagName('div');
                alert(a[0].nodeValue);//这里为什么弹出的事null啊?它不是获取文本内容吗
            }
        </script>
    </head>
    <body>       
        <div>aaa</div>
    </body>
</html>

获取节点的值,上面的@hellnpeter已经给了你个网址,你可以去看看


alert(a[0].nodeValue);
改成
alert(a[0].childNodes[0].nodeValue);

只有文本节点的nodeValue才有值,a[0]是一个DIV节点,它的子节点才是一个文本节点


节点值啊。具体看看这个nodeValue不止可以获取文本节点的还可以获取属性节点值等等http://developer.51cto.com/art/201009/224912.htm

【热门文章】
【热门文章】