innertext , insertadjacentelement , insertadjacenthtml , insertadjacenttext 等區(qū)別
更新時(shí)間:2007年06月29日 00:00:00 作者:
innerText 屬性在 IE 瀏覽器中可以得到當(dāng)前元素過(guò)濾掉 HTML Tags 之后的文本內(nèi)容,在某些時(shí)候還是比較有用。但類似的非標(biāo)準(zhǔn)屬性/方法在其他瀏覽器中并不一定都得到支持。
類似的像 insertAdjacentElement , insertAdjacentElement , insertAdjacentHTML , insertAdjacentText 等。如果需要使用這些非標(biāo)準(zhǔn)的方法,或者已有的代碼大量使用了這些方法的話,就必須為其他瀏覽器提供對(duì)應(yīng)的 prototype 定義。比如:
var isMinNS5 = (navigator.appName.indexOf("Netscape") >= 0 &&
parseFloat(navigator.appVersion) >= 5) ? 1 : 0;
if (isMinNS5){
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);
}
else{
this.parentNode.appendChild(parsedNode)
}
break;
}
}
HTMLElement.prototype.insertAdjacentHTML = function(where,htmlStr){
var r = this.ownerDocument.createRange();
r.setStartBefore(this);
var parsedHTML = r.createContextualFragment(htmlStr);
this.appendChild(parsedHTML)
}
HTMLElement.prototype.insertAdjacentText = function(where,txtStr){
var parsedText = document.createTextNode(txtStr)
this.insertAdjacentElement(where,parsedText)
}
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;
}
);
}
類似的像 insertAdjacentElement , insertAdjacentElement , insertAdjacentHTML , insertAdjacentText 等。如果需要使用這些非標(biāo)準(zhǔn)的方法,或者已有的代碼大量使用了這些方法的話,就必須為其他瀏覽器提供對(duì)應(yīng)的 prototype 定義。比如:
var isMinNS5 = (navigator.appName.indexOf("Netscape") >= 0 &&
parseFloat(navigator.appVersion) >= 5) ? 1 : 0;
if (isMinNS5){
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);
}
else{
this.parentNode.appendChild(parsedNode)
}
break;
}
}
HTMLElement.prototype.insertAdjacentHTML = function(where,htmlStr){
var r = this.ownerDocument.createRange();
r.setStartBefore(this);
var parsedHTML = r.createContextualFragment(htmlStr);
this.appendChild(parsedHTML)
}
HTMLElement.prototype.insertAdjacentText = function(where,txtStr){
var parsedText = document.createTextNode(txtStr)
this.insertAdjacentElement(where,parsedText)
}
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;
}
);
}
相關(guān)文章
zTree獲取當(dāng)前節(jié)點(diǎn)的下一級(jí)子節(jié)點(diǎn)數(shù)實(shí)例
下面小編就為大家?guī)?lái)一篇zTree獲取當(dāng)前節(jié)點(diǎn)的下一級(jí)子節(jié)點(diǎn)數(shù)實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-09-09
原生js實(shí)現(xiàn)計(jì)算購(gòu)物車總金額的示例
本文主要介紹了原生js實(shí)現(xiàn)計(jì)算購(gòu)物車總金額的示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04
Radio 單選JS動(dòng)態(tài)添加的選項(xiàng)onchange事件無(wú)效的解決方法
radio 單選JS動(dòng)態(tài)添加的選項(xiàng),onchange事件無(wú)效。使用delegate()函數(shù)可以解決該問題,具體解決方案大家通過(guò)本文詳細(xì)了解下2016-12-12
Weex開發(fā)之WEEX-EROS開發(fā)踩坑(小結(jié))
這篇文章主要介紹了Weex開發(fā)之WEEX-EROS開發(fā)踩坑(小結(jié)),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10
Javascript類型系統(tǒng)之String字符串類型詳解
這篇文章主要介紹了Javascript類型系統(tǒng)之String字符串類型詳解的相關(guān)資料,需要的朋友可以參考下2016-06-06
uniapp中uni.switchTab無(wú)法傳參的解決辦法
uni.switchTab跳轉(zhuǎn)的必須是TabBar上的路徑,下面這篇文章主要給大家介紹了關(guān)于uniapp中uni.switchTab無(wú)法傳參的解決辦法,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-01-01
淺談關(guān)于JavaScript的語(yǔ)言特性分析
本篇文章小編為大家介紹,淺談關(guān)于JavaScript的語(yǔ)言特性分析,有需要的朋友可以參考一下2013-04-04

