Skip to content

Instantly share code, notes, and snippets.

@tomieric
Created March 19, 2013 09:26
Show Gist options
  • Save tomieric/5194748 to your computer and use it in GitHub Desktop.
Save tomieric/5194748 to your computer and use it in GitHub Desktop.
placeholder占位符 jquery 插件
(function(){
/**
* 占位符
* @param {object} obj lable对象
* @return
*/
$.fn.placeholder = function(obj) {
return $(this).each(function(){
var _self = $(this);
// 获取lable对象,未配置默认按结构查找
var lebleObj = (obj == undefined) ? _self.siblings("lable") : obj;
// 绑定输入框 聚焦和失焦动作
_self.on("focus blur",function(event){
// 获取输入框当前值
var thisValue = $(this).val();
// 判断事件类型分别进行处理
if(event.type == "focus"){
// 输入框聚焦默认隐藏lable
lebleObj.hide();
}else{
// 输入框失焦时根据值是否为空控制lable显示
($.trim(thisValue) == "") ? lebleObj.show() : '';
}
});
// lable对象被点击触发输入框聚焦
lebleObj.on("click",function(){ _self.trigger("focus"); });
});
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment