innerHTML 和 innertext 以及 outerHTML

來源:互聯網
上載者:User

標籤:des   style   blog   java   color   使用   

  今天在製作firefox下支援複製的js代碼的時候,用到了innerText,測試發現原來firefox支援innerHTML但不支援innerText.

  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>

  這段加在你所JS檔案中就可以在MOZILLA/FIREFOX下使用innerText

 HTMLElement.prototype.__defineGetter__ ( "innerText", function () { var anyString = ""; var childS = this.childNodes; for(var i=0; i<childS.length; i++) { if(childS[i].nodeType==1) anyString += childS[i].tagName=="BR" ? ‘\n‘ : childS[i].innerText; else if(childS[i].nodeType==3) anyString += childS[i].nodeValue; } return anyString; } ); 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.