<input type="text" value="" onblur="alert(\"blur\")" onfocus="alert(\"focus\")" id="tt"/>在firefox中执行上面的代码,会发现先执行“失去焦点”事件。
怎样处理这个问题呢?
思路:在“获取焦点”事件中注册“失去焦点”事件,onblur肯定在onfocus后面执行了.<body>
<input type="text" value=""   id="tt"/>
<script>
var txjs = {};
txjs.event = {}; var ua = navigator.userAgent.toLowerCase(); var isStrict = document.compatMode == "CSS1Compat",
isOpera = ua.indexOf("opera") > -1,
isSafari = (/webkit|khtml/).test(ua),
isSafari3 = isSafari && ua.indexOf('webkit/5') != -1,
isIE = !isOpera && ua.indexOf("msie") > -1,
isIE7 = !isOpera && ua.indexOf("msie 7") > -1,
isGecko = !isSafari && ua.indexOf("gecko") > -1,
isBorderBox = isIE && !isStrict,
isWindows = (ua.indexOf("windows") != -1 || ua.indexOf("win32") != -1),
isMac = (ua.indexOf("macintosh") != -1 || ua.indexOf("mac os x") != -1),
isAir = (ua.indexOf("adobeair") != -1),
isLinux = (ua.indexOf("linux") != -1),
isSecure = window.location.href.toLowerCase().indexOf("https") === 0;


    /* TxjsEvent scope */
    //by John Resig
    //url http://ejohn.org/projects/flexible-javascript-events/
    txjs.event.addEvent = function( obj, type, fn ) {
    /// <summary>
    /// 为HTML元素添加事件
    /// </summary>
    /// <param name="obj">
/// 
/// </param>
/// <param name="type">
/// 
/// </param>
/// <param name="fn">
/// 
/// </param>
if ( isIE ) {
 obj['e'+type+fn] = fn;
 obj[type+fn] = function(){obj['e'+type+fn]( window.event );};
 obj.attachEvent( 'on'+type, obj[type+fn] );
} else
 obj.addEventListener( type, fn, false );
};
    
    //by John Resig
    //url http://ejohn.org/projects/flexible-javascript-events/
    txjs.event.removeEvent = function( obj, type, fn ) {
    /// <summary>
    /// 移除HTML元素的事件
    /// </summary>
        if ( isIE ) {
        obj.detachEvent( 'on'+type, obj[type+fn] );
        obj[type+fn] = null;
        } else
            obj.removeEventListener( type, fn, false );
    };
    //失去焦点
function blur()
{
    alert("blur");
    txjs.event.removeEvent(document.getElementById("tt"),"blur",blur);
}//获取焦点
function focus()
{
    alert("focus");
    txjs.event.addEvent(document.getElementById("tt"),"blur",blur);
}
    
//注册focus事件
document.getElementById("tt").onfocus = focus;
</script>
</body>