1. 程式人生 > >innerHTML&innerHTML與innerText的區別&outerHTML&innerHTML與html的區別

innerHTML&innerHTML與innerText的區別&outerHTML&innerHTML與html的區別

實現 對象 logs ava img innertext () cli html

innerHTML

innerHTML 屬性設置或返回表格行的開始和結束標簽之間的 HTML。

語法:HTMLElementObject.innerHTML=text

所有主要瀏覽器都支持 innerHTML 屬性

innerHTML在JS是雙向功能:獲取對象的內容或向對象插入內容;

技術分享圖片

innerHTML 屬性用於設置或返回指定標簽之間的 HTML 內容。  
語法  
   
    Object.innerHTML = "HTML";// 設置  
    var html = Object.innerHTML;// 獲取  
   
例子 1  
   
獲取段落p的 innerHTML(html內容)  
   
    
<html> <head> <script type="text/javascript"> function getInnerHTML(){ alert(document.getElementById("test").innerHTML); } </script> </head><body> <p id="test"><font color="#000">嗨豆殼 www.hi-docs.com</font></p> <input type="
button" onclick="getInnerHTML()" value="點擊" /> </body> </html> 例子 2 設置段落p的 innerHTML(html內容) <html> <head> <script type="text/javascript"> function setInnerHTML(){ document.getElementById("test").innerHTML = "
<strong>設置標簽的html內容</strong>"; } </script> </head><body> <p id="test"><font color="#000">嗨豆殼 www.hi-docs.com</font></p> <input type="button" onclick="setInnerHTML()" value="點擊" /> </body> </html>

document 對象中有innerHTML和innerText 兩個屬性, 這兩個屬性都是獲取document對象的文本內容的,這兩個屬性間有哪些區別呢?通過幾個例子來看一下。

<html>  
    <head><title>innerHTML</title></head>  
    <body>  
        <p id="p1">hello world </p>  
        <script>  
            var content = document.getElementById("p1");  
            alert(content.innerHTML);  
            alert(content.innerText)  
        </script>  
    </body>  
</html>  

通過IE瀏覽器打開,彈出內容為 "hello world" 和 "hello world"

通過 Firefox 瀏覽器打開,彈出內容為 "hello world" 和 "undefined"

通過 chrome 瀏覽器打開,彈出內容為 "hello world" 和 "hello world"

<html>  
    <head><title>innerHTML</title></head>  
    <body>  
        <div id="d1"><p id="p1">hello world </p></div>  
        <script>  
            var content = document.getElementById("d1");  
            alert(content.innerHTML);  
            alert(content.innerText)  
        </script>  
    </body>  
</html>  

通過IE瀏覽器打開,彈出內容為 <p id="p1">hello world </p> 和 hello world

通過 Firefox 瀏覽器打開,彈出內容為 <p id="p1">hello world </p> 和 undefined

通過 chrome 瀏覽器打開,彈出內容為 <p id="p1">hello world </p> 和 hello world

通過上面兩個示例,可以看出:

innerHTML指的是從對象的起始位置到終止位置的全部內容,包括Html標簽。
innerText 指的是從起始位置到終止位置的內容,但它去除Html標簽。
同時,innerHTML 是所有瀏覽器都支持的,innerText 是IE瀏覽器和chrome 瀏覽器支持的,Firefox瀏覽器不支持。其實,innerHTML 是W3C 組織規定的屬性;而innerText 屬性是IE瀏覽器自己的屬性,不過後來的瀏覽器部分實現這個屬性罷了。

說到innerHTML,順便說一下跟innerHTML相對的outerHTML屬性。

繼續看上面的代碼,將alert(content.innerText) 修改為 alert(content.outerHTML)

通過瀏覽器可以看到彈出框為<p id="p1">hello world </p>

和 <divid="d1"><p id="p1">hello world</p></div>

outerHTML指的是除了包含innerHTML的全部內容外, 還包含對象標簽本身。

innerHTML是符合W3C標準的屬性,而innerText只適用於IE瀏覽器(現在也適應chrome瀏覽器),因此,盡可能地去使用 innerHTML,而少用innerText,如果要輸出不含HTML標簽的內容,可以使用innerHTML取得包含HTML標簽的內容後,再用正則表達式去除HTML標簽,下面是一個簡單的符合W3C標準的示例:

<html>  
    <head><title>innerHTML</title></head>  
    <body>  
        <div id="d1"><p id="p1">hello world </p></div>  
        <script>  
            var content = document.getElementById("p1");  
            alert(content.innerHTML.replace(/& lt;.+?>/gim,‘‘));  
        </script>  
    </body>  
</html>  

彈出的為去掉了html標簽之後的內容,這是個在所有瀏覽器均可使用的方法。

InnerHtml() 與html( )的區別

在一個 HTML 文檔中, 我們可以使用 .html() 方法來獲取任意一個元素的內容。 如果選擇器匹配多於一個的元素,那麽只有第一個匹配元素的 HTML 內容會被獲取。

innerHTML 是從對象的起始位置到終止位置的全部內容,包括Html標簽

innerHTML&innerHTML與innerText的區別&outerHTML&innerHTML與html的區別