码迷,mamicode.com
首页 > Web开发 > 详细

jQuery 实现IE10以下浏览器的placeholder效果

时间:2014-09-17 23:17:02      阅读:394      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   color   io   使用   ar   2014   

/*
 *jQuery版本:jQuery-1.8.3.min.js
 *测试的浏览器:IE8,IETester下的IE6-IE9
 */

placeholder是HTML5<input>的属性之一,在不同的浏览器中会有不同的显示效果:

在Chrome( v31.0.1650.63 m)、Firefox( v21.0 )、360安全( v6.3 极速模式 )中,输入栏获得焦点后,提示文字并不消失,如图( Chrome ):

获得焦点前:

bubuko.com,布布扣

获得焦点时:

bubuko.com,布布扣

 

偏偏IE11要搞点特殊:

获得焦点前:

bubuko.com,布布扣

获得焦点时:

bubuko.com,布布扣

也就是说获得焦点时提示的文字会消失。

 

非现代浏览器( 例如 IE6-IE9 )是不支持placeholder属性的。现在用jQuery来使这些非现代浏览器也同样能能实现placeholder的显示效果,第一种方法实现的是IE11这种效果,也就是输入框获得焦点时提示文字会消失;如果要想获得类似Chrome的效果,即输入框获得焦点时提示文字并不消失,可以使用第二种方法,虽然第二种方法有些瑕疵,但是目前没有想出更好的方法来实现这个效果,希望有好方法的朋友赐教。

 

方法一

效果预览:

http://jsfiddle.net/L57b25yr/embedded/result/

思路是,首先判断浏览器是否支持placeholder属性,如果不支持的话,就遍历所有input输入框,获取placeholder属性的值填充进输入框作为提示信息,同时字体设置成灰色。

当输入框获得焦点( focus )同时输入框内文字等于设置的提示信息时,就把输入框内清空;

当输入框失去焦点( blur )同时输入框内文字为空时,再把获取的placeholder属性的值填充进输入框作为提示信息,同时字体设置成灰色;

当输入框内有输入( keydown )时,此时输入框内的提示信息已经由focus事件清除,此时只需要把字体再恢复成黑色即可。

此方法的缺点是,不适用于加载完DOM时即获得焦点的输入框,因为在用户的角度,加载完页面时看到的获得焦点的那个输入框,它的提示文字是看不到的。

HTML:

<input type="text" id="uname" name="uname" placeholder="请输入用户名"/>

CSS:

.phcolor{ color:#999;}

JS:

 1 $(function(){   
 2 
 3     //判断浏览器是否支持placeholder属性
 4     supportPlaceholder=‘placeholder‘in document.createElement(‘input‘),
 5 
 6     placeholder=function(input){
 7 
 8         var text = input.attr(‘placeholder‘),
 9         defaultValue = input.defaultValue;
10 
11         if(!defaultValue){
12 
13             input.val(text).addClass("phcolor");
14         }
15 
16         input.focus(function(){
17 
18             if(input.val() == text){
19     
20                 $(this).val("");
21             }
22         });
23 
24  
25         input.blur(function(){
26 
27             if(input.val() == ""){
28             
29                 $(this).val(text).addClass("phcolor");
30             }
31         });
32 
33         //输入的字符不为灰色
34         input.keydown(function(){
35   
36             $(this).removeClass("phcolor");
37         });
38     };
39 
40     //当浏览器不支持placeholder属性时,调用placeholder函数
41     if(!supportPlaceholder){
42 
43         $(‘input‘).each(function(){
44 
45             text = $(this).attr("placeholder");
46 
47             if($(this).attr("type") == "text"){
48 
49                 placeholder($(this));
50             }
51         });
52     }
53 
54 });

经过测试可以达到IE11placeholder的显示效果。

 

方法二

此方法的思路是做一张含有提示文字的图片作为input输入框的背景图,初始时获得焦点同时加载背景图;

当输入框不为空时,去掉背景图;

当输入框为空时,加载背景图;

当用户键盘按键且输入框不为空( 输入字符 )时,去掉背景图;

当用户键盘按键且输入框为空( 删除字符 )时,加载背景图。

此方法的缺点是:在keyup判断用户有输入时,背景图的消失的时间会稍比文字在输入框中出现的时间晚一点。这是在keyup事件中,当键盘按下时,文字即在输入框中出现,而当按键弹起时,才触发去掉背景图的动作。

HTML代码不变。

CSS:

.phbg{ background:url(img/bg.jpg) 0 0 no-repeat;}

JS:

 1 $(function(){   
 2 
 3           supportPlaceholder=‘placeholder‘ in document.createElement(‘input‘);
 4 
 5           if(!supportPlaceholder){
 6 
 7              $("#uname").attr("class","phbg");
 8              $("#uname").focus;
 9 
10              function destyle(){
11              
12                 if($("#uname").val() != ""){
13                     
14                     $("#uname").removeClass("phbg");
15                 }else{
16                 
17                     $("#uname").attr("class","phbg");
18                  }
19              }
20              
21              destyle();
22 
23              $("#uname").keyup(function(){
24 
25                 destyle();
26              });
27           }
28 });

此方法至此结束。

 

jQuery 实现IE10以下浏览器的placeholder效果

标签:des   style   blog   http   color   io   使用   ar   2014   

原文地址:http://www.cnblogs.com/dee0912/p/3977760.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!