溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

js中value&innerHTML&innerText&textContent之間的區(qū)別有哪些

發(fā)布時(shí)間:2020-09-15 10:19:15 來(lái)源:億速云 閱讀:180 作者:小新 欄目:web開(kāi)發(fā)

這篇文章主要介紹js中value&innerHTML&innerText&textContent之間的區(qū)別有哪些,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

1、value:應(yīng)用于表單的輸入框(textarea除外)

2、innerHTML:可以寫(xiě)入HTML代碼,寫(xiě)入的HTML代碼可以被解析,獲得時(shí)候也可以獲得HTML代碼

3、innerText:獲得內(nèi)容的時(shí)候,會(huì)忽略HTML代碼,寫(xiě)入HTML代碼不能解析。獲得內(nèi)容與HTML解析的內(nèi)容一樣

4、textContent:獲得內(nèi)容的時(shí)候,會(huì)忽略HTML代碼,寫(xiě)入HTML代碼不能解析。獲取的內(nèi)容與源碼內(nèi)容一樣

 <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>修改元素的文本</title>
        <style type="text/css">
            div{
                            width:300px;                
                            height:300px;                
                            float:left;                
                            border:1px solid blue;                
                            margin-left:50px;            
                    
                      }
        </style>
    </head>
    <body>
        <div><p>i love you</p></div>
        <div></div>
        <div></div>
        <div></div>
        <div>
            <p>J                 
            哥                 最    帥</p>
            <a href="http://www.xxoo.com">xx                oo</a>
        </div>
        <div></div>

        <script type="text/javascript">
        var divs=document.getElementsByTagName('div');        /*
            value :應(yīng)用于表單的輸入框---textarea
            innerHTML:與后兩者的區(qū)別,可以寫(xiě)入html代碼會(huì)被解析,并且可以獲得html代碼
            innerText:獲得內(nèi)容的時(shí)候,都會(huì)忽略html代碼
            textContent:獲得內(nèi)容的時(shí)候,都會(huì)忽略html代碼
        */
        //讀取內(nèi)容
        console.log(divs[0].innerHTML);
        console.log(divs[0].innerText);
        console.log(divs[0].textContent);        //寫(xiě)入內(nèi)容
        divs[1].innerHTML='<p>i miss you</p>';
        divs[2].innerText='<p>i miss you</p>';
        divs[3].textContent='<p>i miss you</p>';        /*
            比較innerText和textContent的區(qū)別 
                innerText 獲取的內(nèi)容和html解析的內(nèi)容一樣
                textContent獲取的內(nèi)容與源代碼的內(nèi)容一樣
        */
        console.log('%c'+divs[4].innerText,'color:red;');
        console.log(divs[4].textContent);        var str="<p>哥                 最    帥</p> 
        <a href='http://www.xxoo.com'>xx                oo</a>";        // divs[5].innerText=str;
        // divs[5].textContent=str;

        </script>
    </body>
    </html>

以上是js中value&innerHTML&innerText&textContent之間的區(qū)別有哪些的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI