免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
Firefox 不支持 DOM 對(duì)象的 insertAdjacentHTML insertAdjacentText 方法

標(biāo)準(zhǔn)參考

無(wú)。

問(wèn)題描述

Firefox 不支持 DOM 對(duì)象的 insertAdjacentHTML insertAdjacentText 方法。

造成的影響

使用這兩個(gè)方法將在 Firefox 瀏覽器內(nèi)報(bào)錯(cuò)。

受影響的瀏覽器

Firefox  

問(wèn)題分析

insertAdjacentHTML 方法

insertAdjacentHTML 方法是比 innerHTML、outerHTML 屬性更靈活的插入 HTML 代碼的方法。它可以實(shí)現(xiàn)在一個(gè) DOM 元素的前面、后面、第一個(gè)子元素前面、最后一個(gè)子元素后面四個(gè)位置,插入指定的 HTML 代碼。不是 W3C 標(biāo)準(zhǔn)的 DOM 方法。

這個(gè)方法最初是由 IE4.0 以上版本實(shí)現(xiàn),為私有特性。詳細(xì)內(nèi)容請(qǐng)參見 MSDN 說(shuō)明:insertAdjacentHTML Method。

W3C 近期在 HTML5 草案中擴(kuò)展了這個(gè)方法,更多詳細(xì)的信息,請(qǐng)參見 W3C HTML5 草案:3.5.7 insertAdjacentHTML()

 

目前,主瀏覽器中,只有 Firefox 不支持 insertAdjacentHTML 方法。

<script type="text/javascript">window.onload = function() {var ps = document.getElementById("one");try {ps.insertAdjacentHTML("beforeend", "<span style='background:yellow;'>hi</span>");} catch(err) {document.getElementById("info").innerHTML = err;}}</script><p id="one" style="background:silver; width:300px;">this is<i style="width:100px; background-color:gold;">another</i>text</p><div id="info"></div>

測(cè)試用例中,頁(yè)面加載的時(shí)候,會(huì)執(zhí)行 one 的 insertAdjacentHTML方法。

如果執(zhí)行期間有JS錯(cuò)誤,錯(cuò)誤信息會(huì)在 info 中輸出。

如果執(zhí)行成功,one 元素內(nèi)容的最后會(huì)被加上一個(gè)有黃色背景的文本“hi”。

在各瀏覽器下的截圖:

Firefox IE6 IE7 IE8 Chrome Safari Opera

可見,只有 Firefox 下不支持 insertAdjacentHTML 方法。

 

insertAdjacentText 方法

insertAdjacentText 是比 innerText、outerText 屬性更靈活的插入文本的方法。它可以實(shí)現(xiàn)在一個(gè) DOM 元素的前面、后面、第一個(gè)子元素前面、最后一個(gè)子元素后面四個(gè)位置,插入指定的文本。不是 W3C 標(biāo)準(zhǔn)的 DOM 方法。

這個(gè)方法是 IE 私有特性。詳細(xì)內(nèi)容請(qǐng)參見 MSDN 說(shuō)明:insertAdjacentText Method

至今為止 W3C 的 HTML5 草案還未涉及此方法。

 

目前,主瀏覽器中,只有 Firefox 不支持 insertAdjacentText 方法。

<script type="text/javascript">window.onload = function() {var ps = document.getElementById("one");try {ps.insertAdjacentText("beforeend", "<span style='background:yellow;'>hi</span>");} catch(err) {document.getElementById("info").innerHTML = err;}}</script><p id="one" style="background:silver; width:300px;">this is<i style="width:100px; background-color:gold;">another</i>text</p><div id="info"></div>
  • 測(cè)試用例中,頁(yè)面加載的時(shí)候,會(huì)執(zhí)行 one 的 insertAdjacentText 方法。
  • 如果執(zhí)行期間有 JS 錯(cuò)誤,錯(cuò)誤信息會(huì)在 info 中輸出。

如果執(zhí)行成功,one 元素內(nèi)容的最后會(huì)被加上文本 “<span style='background:yellow;'>hi</span>”。

在各瀏覽器下的截圖:

Firefox IE6 IE7 IE8 Chrome Safari Opera

可見,只有 Firefox 下不支持 insertAdjacentText 方法。

解決方案

在 Firefox 中,可通過(guò)擴(kuò)展 HTMLElement 的原型 (prototype) 來(lái)實(shí)現(xiàn)這兩個(gè)方法:

if (typeof(HTMLElement) != "undefined") {HTMLElement.prototype.insertAdjacentElement = function(where, parsedNode) {switch (where) {case "beforeBegin":this.parentNode.insertBefore(parsedNode, this);break;case "afterBegin":this.insertBefore(parsedNode, this.firstChild);break;case "beforeEnd":this.appendChild(parsedNode);break;case "afterEnd":if (this.nextSibling)this.parentNode.insertBefore(parsedNode, this.nextSibling);elsethis.parentNode.appendChild(parsedNode);break;}}HTMLElement.prototype.insertAdjacentHTML = function(where, htmlStr) {var r = this.ownerDocument.createRange();r.setStartBefore(this);var parsedHTML = r.createContextualFragment(htmlStr);this.insertAdjacentElement(where, parsedHTML);}HTMLElement.prototype.insertAdjacentText = function(where, txtStr) {var parsedText = document.createTextNode(txtStr);this.insertAdjacentElement(where, parsedText);}}

參見

知識(shí)庫(kù)

相關(guān)問(wèn)題

測(cè)試環(huán)境

操作系統(tǒng)版本: Windows 7 Ultimate build 7600
瀏覽器版本: IE6
IE7
IE8
Firefox 3.6.8
Chrome 6.0.472.11 dev
Safari 5.0.1
Opera 10.60
測(cè)試頁(yè)面: insertAdjacentHTML.html
insertAdjacentText.html
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
jquery $(document).ready() 與window.onload的區(qū)別
加快 DHTML 的一組技巧
火狐瀏覽器Firefox收藏夾導(dǎo)入到微軟IE的方法
13個(gè)需要知道的方法:使用 JavaScript 來(lái)操作 DOM
innerText, outerText, innerHTML, outerHTML,insertAdjacentText
傳智播客:Dom模型 - habernate的日志 - 網(wǎng)易博客
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服