今天学JavaScript的DOM时,看到Text属性时,书中有段话:
如果这个文本节点当前存在于文档树中,那么修改文本节点的结果就会立即得到反映。另外,在修改文本节点时还要注意,此时的字符串会经过HTML(或XML,取决于文档类型)编码。换句话说,小于号,大于号或引号都会被转义。
即:div.firstChild.nodeValue = "Some <strong>other</strong> message";
的输出结果是:"Some <strong>other</strong> message"
然而运行完程序后显示的仍然是:Some <strong>other</strong> message
代码:
<!DOCTYPE html>
<html>
<head>
<title>Text Node Example 2</title>
</head>
<body>
<div id="myDiv">Hello world!</div>
<input type="button" value="Change Text" onclick="changeText()">
<script type="text/javascript">
function changeText(){
var div = document.getElementById("myDiv");
div.firstChild.nodeValue = "Some <strong>other</strong> message";
}
</script>
</body>
</html>
原文:http://www.cnblogs.com/zhangty/p/4767918.html