您的位置:首页 > Web前端 > JavaScript

让firefox支持IE的一些方法的javascript扩展函数代码

2010-01-02 00:00 796 查看
这一段使得FireFox也支持IE的innerText方法
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 <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; 
} 
); 
HTMLElement.prototype.__defineSetter__( "innerText", 
function(sText){ 
this.textContent=sText; 
} 
); 
}

这一段使得FireFox的HTMLElement具有click方法(add click method to HTMLElement in Mozilla)
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'); 
}


对HTMLAnchorElement 加入onclick事件
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'); 
}

跟踪回车键事件
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; 
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐