Home > Web Front-end > JS Tutorial > JavaScript extension function code to make firefox support some methods of IE_javascript skills

JavaScript extension function code to make firefox support some methods of IE_javascript skills

WBOY
Release: 2016-05-16 18:37:29
Original
870 people have browsed it
This paragraph enables FireFox to also support IE's innerText method
Copy the code The code is as follows:

function isIE(){
if (window.navigator.userAgent.toLowerCase().indexOf("msie")>=1)
return true;
else
return false ;
}
if(!isIE()){ //firefox innerText define
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;
}
);
HTMLElement.prototype.__defineSetter__( "innerText",
function(sText){
this.textContent=sText;
}
);
}

This paragraph makes FireFox’s HTMLElement have click method (add click method to HTMLElement in Mozilla)
Copy code The code is as follows:

try {
// create span element so that HTMLElement is accessible
document.createElement('span');
HTMLElement.prototype.click = function () {
if (typeof this.onclick == 'function')
this.onclick({type: ' click'});
};
}
catch (e) {
// alert('click method for HTMLElement couldn't be added');
}


Add onclick event to HTMLAnchorElement
Copy the code The code is as follows:

try {
// create a element so that HTMLAnchorElement is accessible
document.createElement('a');
HTMLElement.prototype.click = function () {
if (typeof this.onclick == 'function') {
if (this.onclick({type: 'click'}) && this.href)
window.open(this.href, this.target? this.target : '_self');
}
else if (this.href)
window.open(this.href, this.target? this.target : '_self');
};
}
catch (e) {
// alert('click method for HTMLAnchorElement couldn't be added');
}

Track the Enter key event
Copy code The code is as follows:

function captureKeys (evt ) {
var keyCode = evt.keyCode ? evt.keyCode :
evt.charCode ? evt.charCode : evt.which;
if (keyCode == 13) {
// cancel key:
if (evt.preventDefault) {
evt.preventDefault();
}
var dq = getCookie('default-engine');
if( dq == null) dq = "baidu_txt";
submit_query( dq );
return false;
}
return true;
}
Related labels:
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