[13年迁移]firefoxfocus为火狐新建焦点事件
众所周知,firefox下的element除了有tab线的,其他没有焦点这个概念(你可以给一般元素加tabindex属性使得它有焦点).那没有,为什么不自己做一个.
1 //Just can be running in firefox studio 2 if(document.addEventListener) 3 document.addEventListener("click",function (event){firefoxfocuschange(event);} , false); 4 //The ie brower does not support the function 5 var lastfocus=new Object;//Get a Object to pass the script interpreter 6 var currentfocus=null;//供其他方法使用的焦点设置 7 function firefoxfocuschange(e){ 8 var e=e||window.event;//Compatibility, can be removed,because not to be running in ie studio 9 var newfocus=e.target||e.srcElement;//ditto 10 if(lastfocus!=newfocus){//console.log("focuschange"); 11 if(!lastfocus.tabindex){//console.log("notabindex");//Check element has property for tabindex to prevent running twice. 12 if(lastfocus.onblur){//console.log("runblur"); 13 lastfocus.onblur(); 14 } 15 } 16 } 17 if(currentfocus){ 18 lastfocus=currentfocus;currentfocus=null;}//经测试,ff下,监听事件是在onclick方法执行后发生,那么如果用onclick方法改变焦点,那必须为此问题加入这个currentfocus 19 else{ 20 lastfocus=newfocus;} 21 }
测 试已通过,注释直接英文写的,本人练习英语中,加上这段自制的脚本,可以直接修复firefox下的onblur问题(其实你也可以加入获得focus事 件),可以运用的一些老项目中去,有时间我再吧这个封装一下,变成模块,需要暴露一个onfocus出去(对于高级语言来说等于继承后覆写),在链式查找 上截住以前的设置焦点设置方法,不让他去到window.onfocus,让onfocus都正常,也不知道给不给就是了,让firefox完全和ie下 的focus事件一样.