innerText与innerHTML的区别:
1、innerText将所有文本内容作为普通的文本
2、innerHTML会识别文本内容中是否含有html标签,它能够把html标签的效果显示出来
3、innerText设置<h1>abc</h1>文本时,h1会做为普通文本,
而innerHTML设置<h1>abc</h1>文本时,显示出来的文本只有abc,
但是会以大字体的形式显示出来
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script type="text/javascript" language="javascript">
function setText(){
var content = document.getElementById("txtText").value;
document.getElementById("mySpan").innerText = content;
}
function setHTML(){
var content = document.getElementById("txtHTML").value;
document.getElementById("mySpan").innerHTML = content;
}
</script>
</head>
<body>
<span id="mySpan"></span><br />
<input type="text" id="txtText" />
<input type="button" onclick="setText()" value="显示"/>
<br />
<input type="text" id="txtHTML" />
<input type="button" onclick="setHTML()" value="显示"/>
</body>
</html>