让IE支持placeholder属性
2015-12-15 09:57
477 查看
var JPlaceHolder = { //检测 _check : function(){ return 'placeholder' in document.createElement('input'); }, //初始化 init : function(){ if(!this._check()){ this.fix(); } }, //修复 fix : function(){ jQuery(':input[placeholder]').each(function(index, element) { var self = $(this), txt = self.attr('placeholder'); // 如果当前输入框中有值,不做处理 if (self.val() != '') { // jquery each方法,return true = continue; return false = break; return true; } self.wrap($('<div></div>').css({position:'relative', zoom:'1', border:'none', background:'none', padding:'none', margin:'none'})); var pos = self.position(), h = self.outerHeight(true), paddingleft = self.css('padding-left'); var holder = $('<span></span>').text(txt).css({position:'absolute', left:pos.left, top:'5px', height:h, lienHeight:h, paddingLeft:paddingleft, color:'#aaa'}).appendTo(self.parent()); self.focusin(function(e) { holder.hide(); }).focusout(function(e) { if(!self.val()){ holder.show(); } }); holder.click(function(e) { holder.hide(); self.focus(); }); }); } }; //执行 jQuery(function(){ JPlaceHolder.init(); });
<!doctype html> <html> <head> <meta charset="utf-8"> <title>jQuery JPlaceholder Demo</title> <script src="jquery-1.8.3.min.js"></script> <script src="jquery.JPlaceholder.js"></script> </head> <body> <form> <div> <ul> <li> <input type="text" name="username" placeholder="用户名"> </li> <li> <input type="password" name="username" placeholder="密码"> </li> <li> <button type="button">登录</button> </li> </ul> </div> </form> </body> </html>
相关文章推荐
- IGMP/E-Tree/IPTV
- Java 应用程序打包成.exe
- 在树莓派上创建git仓库
- QT程序发布自动添加QT中依赖的dll
- iOS之NSDate
- 四种方案解决ScrollView嵌套ListView问题
- 【收藏】众多iOS开源资源库
- ASP.NET 获取IP和MAC地址
- linux c 多现线程
- Android SQLiteOpenHelper源码解读
- linux openssh升级步骤
- webdirver学习--怎样得到弹出窗口
- Angular2组件开发—模板的逻辑控制(三)
- HDU 2871 Memory Control
- Libgdx Box2D真实---这缓释微丸(三:规则经常使用body和精灵联合)
- CloudStack 4.4+KVM之管理服务器安装
- python 启动shell报错Subprocess Startup Error
- 侧滑菜单(slidingMenu)
- Java线程堆栈信息查看
- 51单片机AD转换之PCF8591