placeholder 是 html5 的新属性,仅支持 html5 的浏览器才支持 placeholder,目前最新的 FF、Chrome、Safari、Opera 以及 IE10 都支持,IE6-IE9 都不支持!

placeholder 在各个浏览器下基本都是淡灰色显示,不同的地方在于 FF 和 Chrome 中,输入框获得焦点时,placeholder 文字没有变化,只有当输入框中输入了内容时,placeholder 文字才会消失;而在 Safari 和 IE10 下,当输入框获得焦点时,placeholder 文字便立即消失。

js 解决方法:

if( !("placeholder" in document.createElement("input")) ){
$("input[placeholder],textarea[placeholder]").each(function(){
var that = $(this),
text= that.attr("placeholder");
if(that.val()===""){
that.val(text).addClass("placeholder");
}
that.focus(function(){
if(that.val()===text){
that.val("").removeClass("placeholder");
}
})
.blur(function(){
if(that.val()===""){
that.val(text).addClass("placeholder");
}
})
.closest("form").submit(function(){
if(that.val() === text){
that.val("");
}
});
});
}

在页面里引入 jquery,再执行上面这段代码即可!

例子:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>placeholder兼容</title>
</head> <body>
<input type="text" placeholder="我是提示文字"> <!-- 这里 src 改成你自己的 jquery 路径 -->
<script type="text/javascript" src="../scripts/common/jquery-1.8.3.min.js"></script>
<script type="text/javascript">
$(function() {
if( !('placeholder' in document.createElement('input')) ){ $('input[placeholder],textarea[placeholder]').each(function(){
var that = $(this),
text= that.attr('placeholder');
if(that.val()===""){
that.val(text).addClass('placeholder');
}
that.focus(function(){
if(that.val()===text){
that.val("").removeClass('placeholder');
}
})
.blur(function(){
if(that.val()===""){
that.val(text).addClass('placeholder');
}
})
.closest('form').submit(function(){
if(that.val() === text){
that.val('');
}
});
});
}
});
</script>
</body>
</html>

到此,placeholder 兼容问题已解决!

placeholder 兼容 IE的更多相关文章

  1. IE9以下 placeholder兼容

    //input placeholder兼容!(function ($, doc, win) { $.fn.placeholder = function () { var i = doc.createE ...

  2. placeholder兼容

    <!------------placeholder兼容-------------><script type="text/javascript">    $( ...

  3. placeholder兼容方法(兼容IE8以上浏览器)

    //placeholder兼容方法(兼容IE8以上浏览器) var JPlaceHolder = { //检测 _check: function () { return 'placeholder' i ...

  4. IE8 placeholder兼容+Password兼容

    对于placeholder兼容问题 IE系列的大部分不兼容 使用JQ插件解决这个问题,确实用法很简单 jS下载地址http://www.ijquery.cn/js/jquery.placeholder ...

  5. placeholder在不同浏览器下的表现及兼容方法 placeholder兼容

    1.什么是placeholder?    placeholder是html5新增的一个属性,当input或者textarea设置了该属性后,该值的内容将作为灰字提示显示在文本框中,当文本框获得焦点(或 ...

  6. ☆☆☆☆☆Placeholder兼容各大浏览器的例子☆☆☆☆☆

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

  7. input placeholder兼容ie10以下

    代码如下: ,) < ) { $('input[placeholder]').each(function(){ var input = $(this); $(input).val(input.a ...

  8. input placeholder 兼容问题

    placeholder是html5出的新特性,ie9以下是不兼容的, 那么为了兼容ie9  我们需要对他做处理 //jq的处理方式$(function(){ jQuery('[placeholder] ...

  9. ie9 placeholder兼容

    .phcolor{ color:#999;}//css样式 function isPlaceholer(){ var input = document.createElement("inpu ...

随机推荐

  1. JS实现简单倒计时

    /*倒计时*/ lcf.downTime = function (endTime,obj,callback){ /*基本判断*/ if(!endTime || typeof endTime !== & ...

  2. 常用WebService收集

    尊重原著作:本文转载自http://www.cnblogs.com/tianguook/archive/2010/09/29/1838469.html 天气预报Web服务,数据来源于中国气象局Endp ...

  3. TCP/IP详解之:IGMP和DNS

    第13章 IGMP:Internet组管理协议 IGMP用于支持主机和路由器进行多播: IGMP是IP层的一部分,IGMP报文通过IP数据报进行传输: IGMP报文长度为固定8 Byte: 报文中,I ...

  4. python程序不支持中文

    SyntaxError: Non-ASCII character '\xe8' in file delete.py on line 4, but no encoding declared; see h ...

  5. 有了bootstrap,为什么还要做amaze ui

    1.Bootstrap介绍Bootstrap,来自 Twitter,是目前很受欢迎的前端框架.Bootstrap 是基于 HTML.CSS.JAVASCRIPT 的,它简洁灵活,使得 Web 开发更加 ...

  6. SQL Server 的 3 种连接

    第一种 1. nested loop: select * from tableA inner join tableB on tableA.X = tableB.X; 它的执行过程是这样的.对于tabl ...

  7. Linux: service network/Network/NetworkManager

    Linux:service network/Network/NetworkManager start 这三种有什么不同? 1.network service的制御网络接口配置信息改动后,网络服务必须从 ...

  8. Github上的优秀安卓项目

    http://www.cnblogs.com/hawkon/p/3593709.html

  9. Delphi下URL汉字编码解码的两个函数

    //汉字URL编码函数function URLEncode(const S: string; const InQueryString: Boolean): string;var  Idx: Integ ...

  10. Dynamics CRM 2013 初体验(5):Business Rule

    新系统中的Business Rule是个不错的功能,相信它的出现能减少大量的开发工作.在日常开发中,我们需要对记录做大量的业务控制.比如:某字段是否要隐藏,某字段的值是否符合要求以及现实提醒信息等.在 ...