innerText和innerHTML区别简单代码实例

快乐打工仔 分类:实例代码

本章节分享一段代码实例,它演示两个属性的区别。

代码实例如下:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="author" content="http://www.pipipi.net/" />
<title>前端教程网</title>
<style>
div{
  width:200px;
  height:50px;
  background:#CCC;
  margin:5px;
}
</style>
<script type="text/javascript">
window.onload=function(){
  var topDiv=document.getElementById("top");
  var bottomDiv=document.getElementById("bottom");
  topDiv.innerText="<a href='#'>前端教程网</a>";
  bottomDiv.innerHTML="<a href='#'>前端教程网</a>"
}
</script>
</head>
<body><div id="top"></div>
<div id="bottom"></div>
</body>
</html>

上面的代码演示了两者之间的区别。

innerText会把字符串当做普通的文本,里面有html标签也不会解析,但是innerHTML会将html字符串解析。

回复

我来回复
  • 暂无回复内容