首页 > 代码库 > 完美让IE兼容input placeholder属性的jquery实现
完美让IE兼容input placeholder属性的jquery实现
调用时直接引用jquery与下面的js就行了,相对网上的大多数例子来说,这个是比较完美的方案。
/** 球到西山沟* http://www.cnzj5u.com* 2014/11/26 12:12*/(function ($) { //判断是否支持placeholder function isPlaceholer() { var input = document.createElement("input"); return "placeholder" in input; } var placeholderfriend = { focus: function (s) { s = $(s).hide().prev().show().focus(); } } //不支持的代码 if (!isPlaceholer()) { $(function () { var form = $(this); var elements = form.find("input[placeholder]"); elements.each(function (i) { var s = $(this); var pValue = http://www.mamicode.com/s.attr("placeholder"); var sValue =http://www.mamicode.com/ s.val(); if (pValue) { if (sValue =http://www.mamicode.com/="") { //DOM不支持type的修改,需要复制密码框属性,生成新的DOM var html = this.outerHTML || ""; html = html .replace(/\s*type=([‘"])?password\1/gi, " type=\"text\" ") .replace(/\s*value=http://www.mamicode.com/([‘"])?\S*\1?/gi, " value=http://www.mamicode.com/"" + pValue + "\" onfocus=\"phfrfocus(this);\" style=\"color:#a9a9a9;\" "); s.hide(); s.after(html); } } }); elements.blur(function () { var s = $(this); var sValue =http://www.mamicode.com/ s.val(); if (sValue =http://www.mamicode.com/="") { s.hide().next().show(); } }); }); } window.phfrfocus = placeholderfriend.focus;})(jQuery);
完美让IE兼容input placeholder属性的jquery实现
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。