window.onload=function (){
var textdiv=document.getElementById("testdiv");
alert(textdiv.childNodes[0].nodeValue);
}
<div id="testdiv"><em>精彩生活图片库</em></div>很奇怪,弹出来是NULL值textdiv.childNodes[0].nodeValue这一句的不是获取testdiv下的第一个元素中的文本嘛

解决方案 »

  1.   

    textdiv.childNodes[0].childNodes[0].nodeValue 试试
      

  2.   

    L@_@K
    window.onload=function (){
        var textdiv=document.getElementById("testdiv");
        alert(textdiv.childNodes[0].tagName); // EM
        alert(textdiv.childNodes[0].innerText); // 精彩生活图片库
        alert(textdiv.childNodes[0].firstChild.nodeValue); // 精彩生活图片库
    }
    Web 开发常用手册DHTML 参考手册
    http://download.csdn.net/source/308913JScript 语言参考
    http://download.csdn.net/source/308916CCS 样式表中文手册
    http://download.csdn.net/source/304124
      

  3.   

    能说下nodeValue这个属性是什么意思吗?
      

  4.   

    if (node.nodeType == 3)
        node.nodeValue == node.data; ??????? 这个意思?
      

  5.   


        alert(textdiv.childNodes[0].innerText); // 精彩生活图片库
        alert(textdiv.childNodes[0].firstChild.nodeValue); // 精彩生活图片库
    这两个是不是同样的意思
      

  6.   

    nodeValue Property 
    Sets or retrieves the value of a node.object.nodeValue [= sValue]If the object is a TextNode, the nodeValue property returns a string representing the text contained by the node.如果 object 是文本节点,nodeValue 将返回一个表示该节点所包含文本的字符串。If the object is an element, the nodeValue returns null. Use the nodeName property to determine the element name.如果 object 是一个元素节点,nodeValue 将返回 null。使用 nodeName 属性来确定元素名。——以上内容引自《DHTML 参考手册》
      

  7.   

    结果相同,但含义不同!innerText 会忽略 html 标记(如 em);textdiv.childNodes[0].firstChild.nodeValue 与 textdiv.childNodes[0].childNodes[0].nodeValue 含义相同,通过 DOM 树依次查找!