开发者社区 问答 正文

firstChild.nodeValue vs. innerHTML

document.getElementById('msg').firstChild.nodeValue = message;

firstChild.nodeValue为什么会出错?

展开
收起
杨冬芳 2016-06-15 16:18:38 1954 分享 版权
1 条回答
写回答
取消 提交回答
  • IT从业

    善用搜索,楼主,建议去MDN里找解释~.

    firstChild from MDN
    screenshot

    Returns the node's first child in the tree, or null if the node is childless. If the node is a Document, it returns the first node in the list of its direct children.

    你的span确实没有子节点,哪怕给个文本节点如空格回车,都不会返回 null.

    你提到innerHTML这里小提一下,如果说你的span里有多个节点的话,
    这两货的区别大概就如:
    screenshot

    2019-07-17 19:39:37
    赞同 展开评论
问答地址: