兼容ie7以上的 placeholder属性

摘要:
这个项目最近踩过的坑可以绕过,不用考虑ie。使用css3的新属性占位符是多么舒服。然而,邪恶的ie并不支持它。有几种在线方法使用焦点事件,但它们将失去原有的功能。一旦获得焦点或焦点占位符丢失,文本将消失。输入文本时占位符将消失。此方法仅适用于输入[type='text']。不支持其他方法,例如passrod<!教义主义者

最近项目踩过的坑,不考虑ie的可以拐弯绕路走了。

css3的新属性 占位符 placeholder用着多舒服 。

偏偏万恶的ie不支持,网上有几种方法是用焦点事件代替的,不过会失去原有的特性。一旦获取焦点或者失去焦点占位符的文字就会消失。

而placeholder是在输入文字时占位符的文字才会消失

强调一点,此方法只针对 input[type='text'],其他的不支持,比如passwrod

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Document</title>
        <script src="http://s28.9956.cn/static/v3_1/js/jquery.min.js"></script>
    </head>
    <body>
        <input type="" class="clo1" placeholder="电话">
        <script type="text/javascript">//'代码保存成一个js文件引用即可。
            $(document).ready(function() {
                var doc = document,
                inputs = doc.getElementsByTagName('input'),
                supportPlaceholder = 'placeholder' in doc.createElement('input'),

                placeholder = function(input) {
                    var text = input.getAttribute('placeholder'),
                    defaultValue = input.defaultValue;
                    if (defaultValue == '') {
                        input.value = text
                    }
                    input.onfocus = function() {
                        if (input.value === text) {
                            this.value = ''
                        }
                    };
                    input.onblur = function() {
                        if (input.value === '') {
                            this.value = text
                        }
                    }
                };

                if (!supportPlaceholder) {
                    for (var i = 0,
                    len = inputs.length; i < len; i++) {
                        var input = inputs[i],
                        text = input.getAttribute('placeholder');
                        if (input.type === 'text' && text) {
                            placeholder(input)
                        }
                    }
                }
            });
        </script>
    </body>
</html>

再列举一个支持passwrod的方法,比较猥琐,用一个label标签把input包起来,里面在加一个标签代替placeholder的文字

先做个样式,定位起来

<style type="text/css">
    label{position: relative;}
    label p{position: absolute;left: 5px;top:0px;margin: 0;padding: 0;}
</style>
<label>
    <input onfocus="$(this).siblings('p').hide();" onblur="if($(this).val()==''){$(this).siblings('p').show();}" type="password" class="psw">
    <p style="display: block;">请输入密码</p>
</label>

上面用的js方法写的,写到标签里的,大家喜欢jquery的写法的话,请看下面代码

<script type="text/javascript">
    $(".psw").focus(function(){
        $("p").hide();
    });
    $(".psw").blur(function(){
        $("p").show();
    });
</script>

免责声明:文章转载自《兼容ie7以上的 placeholder属性》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Python机器学习(6)——逻辑回归分类一个可以选择目录生成doc目录内容的小工具(三) -python-docx下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Xamarin.Forms一些常见问题

安装 1、查看Xaramin.Forms的版本 在vs项目中查看引用的包(Xamarin.Forms)的版本,或者直接进文件夹看 C:MicrosoftXamarinNuGetxamarin.forms 可以通过 NuGet 更新到最新的 Xamarin 版本。 开发 XAML 1、xaml中用到的StaticResource的定义 <Setter...

【使用 DOM】为DOM元素设置样式

1. 使用样式表 可以通过document.styleSheets属性访问文档中可用的CSS样式表,它会返回一组对象集合,这些对象代表了与文档管理的各个样式表。 每个样式表 都由一个CSSStyleSheet 对象代表,它提供了一组属性和方法来操作文档里的样式。 1.1 获得样式表的基本信息 第一步是获得定义在文档中的样式表的一些基本信息。 <!...

bootstrap基础(二)

表单 一、基础表单 表单中常见的元素主要包括:文本输入框、下拉选择框、单选按钮、复选按钮、文本域和按钮等。 二、水平表单 在<form>元素上使用类名“form-horizontal” <form class="form-horizontal" role="form"> <div class="form-group">...

bootstrap-table:操作栏点击编辑按钮弹出模态框修改数据

核心代码: columns: [ { checkbox:true //第一列显示复选框 },                  ......

javascript占位符

传统javascript: The text is inserted in the value attribute. On focus, it checks if the value is "search" and returns empty to clear the field. If the value is empty, it returns "se...

微信小程序实现动态设置placeholder提示文字

xml <input placeholder='{{phValue}}' type="text"  bindfocus='onFocus' bindblur='onBlur'/>   js Page({ // 页面的初始数据 data: { phValue:"请输入要录入的单词" }, onFocus: function (e) { this...