兼容ie7以上的 placeholder属性
最近项目踩过的坑,不考虑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:;padding:;}
</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属性的更多相关文章
- 改进《完美让IE兼容input placeholder属性的jquery实现》的不完美
<完美让IE兼容input placeholder属性的jquery实现>中的代码在IE9以下浏览器中会出错,原因是因为ie9以下的浏览器对input的标签的解释不同. 例如对以下文本框的 ...
- IE如何兼容placeholder属性
在前端开发中,经常需要为input设置placeholder属性,但是placeholder是HTML5新属性,在IE10以下不兼容,那么如何完美兼容呢? 网上搜索了一下,其实也挺简单的,可以采用以下 ...
- input 的 placeholder属性在IE8下的兼容处理
placeholder是input标签的新属性,在使用的时候有两个问题: 1.IE8 下不兼容 处理思路: 如果浏览器不识别placeholder属性,给input添加类名placeholder,模仿 ...
- HTML5表单提示placeholder属性兼容IE
placeholder 属性提供可描述输入字段预期值的提示信息(hint). 该提示会在输入字段为空时显示,并会在字段获得焦点时消失. 注释:placeholder 属性适用于以下的 <inpu ...
- 【工作笔记五】html5的placeholder属性(IE如何兼容placeholder属性)
placeholder属性,IE对其的支持表示无奈,firefox.google chrome表示毫无压力. HTML5对Web Form做了许多增强,比如input新增的type类型.Form Va ...
- placeholder属性兼容ie8
<!doctype html> <html> <head> <meta charset="utf-8" /> <title&g ...
- 兼容ie10以下版本的placeholder属性
<script src="${ctx }/js/jquery.placeholder.js" type="text/javascript">< ...
- Html5的placeholder属性(IE兼容)
HTML5对Web Form做了很多增强,比方input新增的type类型.Form Validation等. Placeholder是HTML5新增的还有一个属性,当input或者textarea设 ...
- html5 placeholder属性兼容ie11
placeholder 属性是html5的属性,用于提供描述输入字段预期值的提示信息(hint). 简单例子: <!DOCTYPE HTML> <html> <body& ...
随机推荐
- 2017-12-30-如何彻底清除现存GIT仓库的大量提交历史
layout: post title: 2017-12-30-如何彻底清除现存GIT仓库的大量提交历史 key: 20171230 tags: GIT 版本管理 问答 modify_date: 201 ...
- php之快速排序
<?phpfunction shell_sort(array $arr){ $right=$left = array(); $Rights=$Lefts = array() ...
- 【PHP】 PHPqrCode二维码类库使用方法
1.首先去官网下载PHPqrCode库文件,只需要里面的phpqrcode.php文件,下载地址:http://phpqrcode.sourceforge.net 2.二维码生成实例代码: <? ...
- java.lang.String中[ "张飞"+1+1 ] 和 [ "张飞"+(1+1) ]
废话不多说,上代码: package com.core; public class StringTest { public static void main(String[] args) { Stri ...
- 读Ghost博客源码与自定义Ghost博客主题
我使用的Ghost博客一直使用者默认的Casper主题.我向来没怎么打理过自己博客,一方面认为自己不够专业,很难写出质量比较高的文字:另一方面认为博客太耗时间,很容易影响正常的工作内容.最近公司即将搬 ...
- C++数据结构学习之顺序表
顺序表是数据结构中最基本也是应用相当广泛的一种数据结构类型.它通常包含三个私有成分,即指向数据数组的头指针.当前表长以及表的实际容量.表的头指针通常指向数据数组的基地址,通过数组的形式进行访问数据数组 ...
- python数据分析工具包(2)——Numpy(二)
上一篇文章简单地介绍了numpy的一些基本数据类型,以及生成数组和矩阵的操作.下面我们来看一下矩阵的基本运算.在线性代数中,常见的矩阵运算包括,计算行列式.求逆矩阵.矩阵的秩等.下面我们来一一实现. ...
- Hive metastore源码阅读(二)
最近随着项目的深入,发现hive meta有些弊端,就是你会发现它的元数据操作与操作物理集群的代码耦合在一起,非常不利于扩展.比如:在create_table的时候同时进行路径校验及创建,如下代码: ...
- ansible 拷贝文件并重启服务
Ansible 安装 只需要在ansible 服务器上安装 yum install -y epel-release yum install -y ansible 服务器生成密钥对 ssh-ke ...
- 设置修改CentOS系统时区
一.时区 1. 查看当前时区date -R 2. 修改设置时区方法(1)tzselect方法(2) 仅限于RedHat Linux 和 CentOS系统timeconfig方法(3) 适用于Debia ...