zoukankan      html  css  js  c++  java
  • [转]innerHtml,innerText,outterHtml,outterText 的区别

    js中innerHTML与innerText的用法与区别

    用法:

    Java代码 复制代码 收藏代码
    1. <div id="test">   
    2.    <span style="color:red">test1</span> test2   
    3. </div>  

    在JS中可以使用:
    Java代码 复制代码 收藏代码
    1. test.innerHTML:  

      也就是从对象的起始位置到终止位置的全部内容,包括Html标签。

      上例中的test.innerHTML的值也就是“<span style="color:red">test1</span> test2 ”。

    test.innerText:

      从起始位置到终止位置的内容, 但它去除Html标签

      上例中的text.innerTest的值也就是“test1 test2”, 其中span标签去除了。

    test.outerHTML:

      除了包含innerHTML的全部内容外, 还包含对象标签本身。

      上例中的text.outerHTML的值也就是<div id="test"><span style="color:red">test1</span> test2</div>


    完整示例:
    Java代码 复制代码 收藏代码
    1. <div id="test">   
    2.    <span style="color:red">test1</span> test2   
    3. </div>   
    4.   
    5. <a href="javascript:alert(test.innerHTML)">innerHTML内容</a>   
    6. <a href="javascript:alert(test.innerText)">inerHTML内容</a>   
    7. <a href="javascript:alert(test.outerHTML)">outerHTML内容</a>  

    特别说明:

      innerHTML是符合W3C标准的属性,而innerText只适用于IE浏览器,因此,尽可能地去使用innerHTML,而少用innerText,如果要输出不含HTML标签的内容,可以使用innerHTML取得包含HTML标签的内容后,再用正则表达式去除HTML标签,下面是一个简单的符合W3C标准的示例:

    <a href="javascript:alert(document.getElementById('test').innerHTML.replace(/<.+?>/gim,''))">无HTML,符合W3C标准</a>

    -------------------------------------------------------------------------------------------------------------------------------
    Java代码 复制代码 收藏代码
    1. <html>   
    2. <head></head>   
    3. <frameset frameborder="yes" frameborder="1" rows="40%,*">   
    4. <frame name="top" src="1.html">   
    5. <frame name="bottom" src="2.html">   
    6. </frameset>   
    7. </html>   
    8.   
    9. <html>   
    10. <head>   
    11. <script language="javascript">   
    12. function init()   
    13. {       
    14.     var aaa = parent.window.frames[0].document.body.innerHTML;    
    15.     alert(aaa);   
    16. }   
    17. </script>   
    18. </head>   
    19. <body>   
    20. <p align="center">nothing</p>   
    21. <p align="center"><input type="button" onclick="init()"; value="click"></p>   
    22. </body>   
    23. </html>   
    24.   
    25. <html>   
    26. <center>汽车 房产 女人</center>   
    27. </html>  

    运行下面的脚本看看效果。

    <body> 
        <div id=test> 
            <table> 
                <tr> 
                    <td> 
                          CCTV<a>中国</a>中央电视台  
                    </td> 
                </tr> 
            </table> 
        </div> 
        <input type=button onclick=alert(test.innerText) value="show innerText"> 
        <br> 
        <input type=button onclick=alert(test.innerHTML) value="show innerHTML"> 
        <br> 
        <input type=button onclick=alert(test.outerHTML) value="show outerHTML"> 
        <br> 
        <hr> 
        <br> 
        <div id="div"> 
            <input name="button" value="Button" type="text"> 
            <font color="green"> 
                <h2>This is a DIV!</h2> 
            </font> 
        </div> 
        <input name="innerHTML" value="innerHTML" type="button" OnClick="alert(div.innerHTML);"> 
        <input name="outerHTML" value="outerHTML" type="button" OnClick="alert(div.outerHTML);"> 
        <input name="innerText" value="innerText" type="button" OnClick="alert(div.innerText);"> 
        <input name="outerText" value="outerText" type="button" OnClick="alert(div.outerText);"> 
    </body>

  • 相关阅读:
    将绿色版Tomcat服务添加到系统服务并设为开机运行
    简单的递归遍历树
    js浏览器中的alert死浏览器
    Crontab文件的参数【转载】
    修改tomcat项目的图标
    最后两个and半月
    没有信的信乐团,依然让我动情
    The Network Adapter could not establish the connec
    MySql数据库的备份和恢复
    extjs
  • 原文地址:https://www.cnblogs.com/furenjun/p/2192078.html
Copyright © 2011-2022 走看看