jQuery focus、blur事件 添加、刪除類名

jQuery.focusblur = function(ele,className){
    var focusblurid = $(ele);
    focusblurid.focus(function(){
        var attr = focusblurid.attr("data-class");
        if(!attr){
            focusblurid.parent().children().addClass(className);//根據本身須要添加類名
            focusblurid.attr("data-class",true);//添加自定義屬性 判斷是否類名存在
        }
    });
    focusblurid.blur(function(){
        var attr = focusblurid.attr("data-class");
        if(attr){
            focusblurid.parent().children().removeClass(className);
            focusblurid.removeAttr("data-class",true);
        }
    });
}
$.focusblur("#x_phone","x-name-fucus");//參數爲 元素選擇器名   添加的類名
$.focusblur("#x_pwd","x-pwd-fucus");
相關文章
相關標籤/搜索