您的位置:首页 > Web前端 > HTML

问题:修改文本节点时字符串会经过HTML编码?

2015-08-28 21:23 507 查看
今天学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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: