首页 > 编程语言 > 详细

JavaScript中innerText和innerHTML的区别

时间:2015-02-28 14:13:46      阅读:230      评论:0      收藏:0      [点我收藏+]

案例

<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
  <script type="text/javascript">
    onload=function(){
     //把所有内容全部设置到层中。
     document.getElementById(‘btn1‘).onclick=function(){
           document.getElementById(‘dv‘).innerText=‘<h1>标题</h1>‘;
     };
        //把效果直接显示出来
     document.getElementById(‘btn2‘).onclick=function(){
           document.getElementById(‘dv‘).innerHTML=‘<h1>标题</h1>‘;
     };
        //只获取文字内容
     document.getElementById(‘btn3‘).onclick=function(){
      alert( document.getElementById(‘dv‘).innerText);
     };
        //获取该效果的所有标签和文本
     document.getElementById(‘btn4‘).onclick=function(){
      alert( document.getElementById(‘dv‘).innerHTML);
     };           
    };
  </script>
</head>
<body>
<div id="dv">
</div>
<input type="button" id="btn1" value="设置innerText"/>
<input type="button" id="btn2" value="设置innerHtml"/>
<input type="button" id="btn3" value="获取innerText"/>
<input type="button" id="btn4" value="获取innerHtml"/>
</body>
</html>
技术分享

JavaScript中innerText和innerHTML的区别

原文:http://www.cnblogs.com/mcad/p/4305176.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!