Enabling Firefox to support innertext implementation code _javascript Tips

Source: Internet
Author: User
Tags tagname
Implementing InnerText Properties for Firefox
A lot of code written and forgot to write, very wasteful, so decided to develop the habit of making notes.
Knowledge Points:
0, why should innertext? Because of security.
1. Extended properties for the Firefox DOM model
2, the Currentstyle property can get the actual style state
3, IE implemented innertext when the display mode, if the block is added line
4, why not textcontent? Because Textcontent does not consider the element display way, so not completely with IE compatible
Copy Code code as follows:

<body>
<div id= "D1" ><a href= "AA" >ccc</a>ddd<div>eeee</div>fff</div>
<script type= "Text/javascript" >
<!--
//
Patch of innertext for Firefox
//
(function (bool) {
function Setinnertext (o, s) {
while (o.childnodes.length!= 0) {
O.removechild (O.childnodes[0]);
}
O.appendchild (document.createTextNode (s));
}
function Getinnertext (o) {
var sret = "";
for (var i = 0; i < o.childnodes.length i + +) {
if (o.childnodes[i].childnodes.length!= 0) {
Sret + + getinnertext (o.childnodes[i]);
}
if (O.childnodes[i].nodevalue) {
if (O.currentstyle.display = = "Block") {
Sret + = O.childnodes[i].nodevalue + "\ n";
} else {
Sret + = O.childnodes[i].nodevalue;
}
}
}
return sret;
}
if (bool) {
htmlelement.prototype.__definegetter__ ("Currentstyle", function () {
Return This.ownerDocument.defaultView.getComputedStyle (this, null);
});
htmlelement.prototype.__definegetter__ ("InnerText", function () {
Return Getinnertext (this);
})
htmlelement.prototype.__definesetter__ ("InnerText", function (s) {
Setinnertext (this, s);
})
}
}) (/firefox/.test (window.navigator.userAgent));
-->
</script>
<script type= "Text/javascript" >
<!--
var D1 = document.getElementById ("D1");
alert (D1.innertext);
D1.innertext = "xxx";
-->
</script>
</body>


Today in the production of Firefox to support the replication of the JS code, used to innertext, the test found that the original Firefox support innerHTML but do not support innertext, so the internet looked for a moment, found a very good code. From the reply, we get the following compatibility code. Fixed the original IE under the error prompted the problem. A specific look at the article.

Add this paragraph to your JS file and you can use it under Mozilla/firefox innertext
Copy Code code as follows:

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


But this code in IE, it will prompt htmlelement undefined, the following is the specific solution.

Copy Code code as follows:

function Isie () {//ie? Determine whether IE
if (window.navigator.userAgent.indexOf ( "MSIE") >=1
return true;
Else
return false;
}
if (!isie ()) {
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== "B R "? ' \ n ': childs[i].innertext;
Else if (childs[i].nodetype==3)
AnyString + = Childs[i].nodevalue;
}
Return anystring;
}
);
}

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.