Home >Web Front-end >JS Tutorial >Let FireFox support innerText implementation code_javascript skills

Let FireFox support innerText implementation code_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:40:471201browse
Implement innerText attribute for firefox
A lot of code was written and forgotten again, which was very wasteful, so I decided to develop the habit of taking notes.
Knowledge points:
0. Why innerText? Because of security issues
1. Extend attributes for the firefox dom model
2. The currentStyle attribute can obtain the actual style status
3. When IE implements innerText, it considers the display method. If it is a block, a newline is added
4. Why not use textContent? Because textContent does not consider the display mode of the element, it is not fully compatible with IE
Copy code The code is as follows:



cccddd
eeee

< script type="text/javascript">






Today When making js code that supports copying under firefox, innerText was used. The test found that firefox supports innerHTML but not innerText, so I searched online and found a very good code. In addition, from the reply, we got the following compatibility code. Fixed the original problem of error prompt under IE. See the article below for details.

Add this paragraph to your JS file and you can use innerText under MOZILLA/FIREFOX
Copy code The code is as follows:

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


But this code will prompt that HTMLElement is undefined in IE, as follows It’s a concrete solution.

Copy code The code is as follows:

function isIE(){ //ie? Determine whether it is 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{
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;
}
);
}
Statement:
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