Home > Web Front-end > JS Tutorial > Differences between innertext, insertadjacentelement, insertadjacenthtml, insertadjacenttext, etc._javascript skills

Differences between innertext, insertadjacentelement, insertadjacenthtml, insertadjacenttext, etc._javascript skills

WBOY
Release: 2016-05-16 19:12:03
Original
1420 people have browsed it

The innerText attribute can be used to obtain the text content of the current element after filtering out HTML Tags in the IE browser, which is quite useful in some cases. However, similar non-standard properties/methods are not necessarily supported in other browsers.
Similar ones like insertAdjacentElement , insertAdjacentElement , insertAdjacentHTML , insertAdjacentText etc. If you need to use these non-standard methods, or if the existing code uses these methods extensively, you must provide corresponding prototype definitions for other browsers. for example:


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 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;
}
);
}

Related labels:
ins
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template