最近项目踩过的坑,不考虑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属性的更多相关文章

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

    <完美让IE兼容input placeholder属性的jquery实现>中的代码在IE9以下浏览器中会出错,原因是因为ie9以下的浏览器对input的标签的解释不同. 例如对以下文本框的 ...

  2. IE如何兼容placeholder属性

    在前端开发中,经常需要为input设置placeholder属性,但是placeholder是HTML5新属性,在IE10以下不兼容,那么如何完美兼容呢? 网上搜索了一下,其实也挺简单的,可以采用以下 ...

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

    placeholder是input标签的新属性,在使用的时候有两个问题: 1.IE8 下不兼容 处理思路: 如果浏览器不识别placeholder属性,给input添加类名placeholder,模仿 ...

  4. HTML5表单提示placeholder属性兼容IE

    placeholder 属性提供可描述输入字段预期值的提示信息(hint). 该提示会在输入字段为空时显示,并会在字段获得焦点时消失. 注释:placeholder 属性适用于以下的 <inpu ...

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

    placeholder属性,IE对其的支持表示无奈,firefox.google chrome表示毫无压力. HTML5对Web Form做了许多增强,比如input新增的type类型.Form Va ...

  6. placeholder属性兼容ie8

    <!doctype html> <html> <head> <meta charset="utf-8" /> <title&g ...

  7. 兼容ie10以下版本的placeholder属性

    <script src="${ctx }/js/jquery.placeholder.js" type="text/javascript">< ...

  8. Html5的placeholder属性(IE兼容)

    HTML5对Web Form做了很多增强,比方input新增的type类型.Form Validation等. Placeholder是HTML5新增的还有一个属性,当input或者textarea设 ...

  9. html5 placeholder属性兼容ie11

    placeholder 属性是html5的属性,用于提供描述输入字段预期值的提示信息(hint). 简单例子: <!DOCTYPE HTML> <html> <body& ...

随机推荐

  1. backupMysql.sh

    #!/bin/sh #!/bin/bash function backup() { for i in $* do mysqldump -h$hostip -P$port -u$username -p$ ...

  2. 【转】IGS相关

    目 前,全球260多个lGS跟踪站中,我国占20多个,分布在武汉.拉萨.乌鲁木齐.昆明.上海等地,全球IGS网的GPS数据,由单台接收机交换 (RINEX)格式生成的日观测和导航数据文件组成,其存储方 ...

  3. 用Lua定制Redis命令

    * { color: #3e3e3e } body { font-family: "Helvetica Neue", Helvetica, "Hiragino Sans ...

  4. java中的二叉树排序问题

    原创:转载请注明出处 目的:想用java实现二叉树排序算法 思想:利用java中面向对象的思想,即: Tree:类 树根Tree:root //static所属于每一个Tree 左节点Tree:lef ...

  5. 正则匹配url中的query参数信息

    var url = 'name=xiaoming&age=10&school=xinhua'; var reg = /([^&=]+)=?([^&]*)/g;

  6. window.print打印指定html元素中的内容

    通常有些时候我们项目过程中使用到打印功能,而wndow.print便是系统里提供的一个函数. 但是直接使用的话,它打印的将是整个页面的所有元素,而有些时候我们又只需要打印部分内容. <body& ...

  7. phpstorm中配置真正的远程调试(xdebug)

    这里说的是真正的远程调试,不是本地,本地不需要安装任何php程序!!! 这里略去xdebug的安装,安装很简单可以下载源码包,动态编译进去! 环境: Dev 服务器(IP:192.168.2.100) ...

  8. Windows Azure Storage (25) Azure Append Blob

    <Windows Azure Platform 系列文章目录> 在笔者之前的文章中,我们介绍了Azure Blob 有两种:Block Blob和Page Blob. 在这里笔者介绍Blo ...

  9. qt中的多线程

    1.dialog.h #define DIALOG_H #include <QDialog>#include"mythread.h"namespace Ui {clas ...

  10. qt安装--this Qt version uses an unsupported makefile

    解决办法: Run regedit. Hop to HKEY_CURRENT_USER\Software\Trolltech\Versions或HKEY_CURRENT_USER\Software\D ...