完美让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 = s.attr("placeholder");
                var sValue = s.val();
                if (pValue) {
                    if (sValue == "") {
                        //DOM不支持type的修改,需要复制密码框属性,生成新的DOM
                        var html = this.outerHTML || "";
                        html = html
                            .replace(/\s*type=([‘"])?password\1/gi, " type=\"text\" ")
                            .replace(/\s*value=([‘"])?\S*\1?/gi, " value=\"" + pValue + "\" onfocus=\"phfrfocus(this);\" style=\"color:#a9a9a9;\" ");
                        s.hide();
                        s.after(html);
                    }
                }
            });

            elements.blur(function () {
                var s = $(this);
                var sValue = s.val();
                if (sValue == "") {
                    s.hide().next().show();
                }
            });

        });
    }
    window.phfrfocus = placeholderfriend.focus;
})(jQuery);
时间: 2024-08-28 12:07:08

完美让IE兼容input placeholder属性的jquery实现的相关文章

改进《完美让IE兼容input placeholder属性的jquery实现》的不完美

<完美让IE兼容input placeholder属性的jquery实现>中的代码在IE9以下浏览器中会出错,原因是因为ie9以下的浏览器对input的标签的解释不同. 例如对以下文本框的解释: <input id="itxt" class="itext" type="text" title="这是一个文本框" value="点我输入内容" tabindex="1" m

让 IE 8 及以下浏览器支持 表单 input [placeholder] 属性

在我们的日常开发中,时常会遇到要求兼容 ie低版本 的项目,其中表单多的项目,其中 用到 placeholder 属性几乎是必不可少的. placeholder是一个很实用的Html 5属性.但是该属性在低版本的IE下是无效的. 于是在网上找了很多方法,就以下方法,比较简单方便. 首先新建一个js文件,把下方代码,放到一个JS文件里面,页面直接引入即可. //解决ie下 input 的placeholder不显示问题 var JPlaceHolder = { //检测 _check: funct

HTML 5 &lt;input&gt; placeholder 属性

<input placeholder="请先选择组织" type="text" value="" </input> placeholder 属性提供可描述输入字段预期值的提示信息(hint). 该提示会在输入字段为空时显示,并会在字段获得焦点时消失. 注释:placeholder 属性适用于以下的 <input> 类型:text, search, url, telephone, email 以及 password.

input placeholder属性IE、360浏览器兼容性问题

效果:http://hovertree.com/texiao/jquery/43/ 效果二:http://hovertree.com/texiao/jquery/43/1/ 请在IE中体验. 1.创建JS文件:jquery.JPlaceholder.js js代码如下: /* * jQuery placeholder, fix for IE6,7,8,9 * hovertree.com */ var JPlaceHolder = { //检测 _check : function(){ retur

input 的 placeholder属性在IE8下的兼容处理

placeholder是input标签的新属性,在使用的时候有两个问题: 1.IE8 下不兼容 处理思路: 如果浏览器不识别placeholder属性,给input添加类名placeholder,模仿placeholder属性的样式,并给input 的value赋值placeholder属性的值 2. input的type属性是password的情况,用上面的方法处理提示语为密码文 处理思路: 新添加一个标签,模仿placeholder属性 直接上代码: css部分: 1 .input-item

兼容ie10以下版本的placeholder属性

<script src="${ctx }/js/jquery.placeholder.js" type="text/javascript"></script> $(function() { $('input').placeholder();//兼容ie10下placeholder属性: }); js文件地址

Html5的placeholder属性(IE兼容)

HTML5对Web Form做了很多增强,比方input新增的type类型.Form Validation等. Placeholder是HTML5新增的还有一个属性,当input或者textarea设置了该属性后.该值的内容将作为灰字提示显示在文本框中,当文本框获得焦点时,提示文字消失.曾经要实现这效果都是用JavaScript来控制才干实现 , firefox.google chrome等表示对其支持 , 只有IE存在违和感啊! 比如:  <input id="t1" type

placeholder属性兼容ie8

<!doctype html> <html> <head> <meta charset="utf-8" /> <title>登陆框的制作</title> <script src="jquery-1.11.3.js"></script> <script src="jquery.cookie.js"></script> <sc

【工作笔记五】html5的placeholder属性(IE如何兼容placeholder属性)

placeholder属性,IE对其的支持表示无奈,firefox.google chrome表示毫无压力. HTML5对Web Form做了许多增强,比如input新增的type类型.Form Validation等.Placeholder是HTML5新增的另一个属性,当input或者textarea设置了该属性后,该值的内容将作为灰字提示显示在文本框中,当文本框获得焦点时,提示文字消失.以前要实现这效果都是用JavaScript来控制才能实现: <input id="t1"