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");