Innertext, insertadjacentelement, insertadjacenthtml, insertadjacenttext, and other differences _ javascript skills

Source: Internet
Author: User
Innertext, insertadjacentelement, insertadjacenthtml, insertadjacenttext, and other differences innerText attributes can be used in IE browsers to obtain the text content filtered out by the current element after HTML Tags, which is useful in some cases. However, similar non-standard attributes/methods are not necessarily supported in other browsers.
Such as insertAdjacentElement, insertAdjacentElement, insertAdjacentHTML, and insertAdjacentText. If you need to use these non-standard methods or the existing Code uses these methods in large quantities, you must provide 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 Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.