亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

兩種方法基于jQuery實(shí)現(xiàn)IE瀏覽器兼容placeholder效果

 更新時(shí)間:2014年10月14日 11:33:25   投稿:hebedich  
這篇文章主要介紹了兩種方法基于jQuery實(shí)現(xiàn)IE瀏覽器兼容placeholder效果,需要的朋友可以參考下

placeholder是HTML5<input>的屬性之一,在不同的瀏覽器( 支持HTML5的現(xiàn)代瀏覽器 )中會(huì)有略微不同的顯示效果:

在Chrome( v31.0.1650.63 m)、Firefox( v21.0 )、360安全( v6.3 極速模式 )中,輸入欄獲得焦點(diǎn)后,提示文字并不消失,如圖( Chrome ):

獲得焦點(diǎn)前:

獲得焦點(diǎn)時(shí):

偏偏I(xiàn)E11要搞點(diǎn)特殊:

獲得焦點(diǎn)前:

獲得焦點(diǎn)時(shí):

也就是說(shuō)獲得焦點(diǎn)時(shí)提示的文字會(huì)消失。

非現(xiàn)代瀏覽器( 例如 IE6-IE9 )是不支持placeholder屬性的?,F(xiàn)在用jQuery來(lái)使這些非現(xiàn)代瀏覽器也同樣能能實(shí)現(xiàn)placeholder的顯示效果,第一種方法實(shí)現(xiàn)的是IE11這種效果,也就是輸入框獲得焦點(diǎn)時(shí)提示文字會(huì)消失;如果要想獲得類(lèi)似Chrome的效果,即輸入框獲得焦點(diǎn)時(shí)提示文字并不消失,可以使用第二種方法。

方法一

效果預(yù)覽:

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

思路是,首先判斷瀏覽器是否支持placeholder屬性,如果不支持的話,就遍歷所有input輸入框,獲取placeholder屬性的值填充進(jìn)輸入框作為提示信息,同時(shí)字體設(shè)置成灰色。

當(dāng)輸入框獲得焦點(diǎn)( focus )同時(shí)輸入框內(nèi)文字等于設(shè)置的提示信息時(shí),就把輸入框內(nèi)清空;

當(dāng)輸入框失去焦點(diǎn)( blur )同時(shí)輸入框內(nèi)文字為空時(shí),再把獲取的placeholder屬性的值填充進(jìn)輸入框作為提示信息,同時(shí)字體設(shè)置成灰色;

當(dāng)輸入框內(nèi)有輸入( keydown )時(shí),此時(shí)輸入框內(nèi)的提示信息已經(jīng)由focus事件清除,此時(shí)只需要把字體再恢復(fù)成黑色即可。

此方法的缺點(diǎn)是,不適用于加載完DOM時(shí)即獲得焦點(diǎn)的輸入框,因?yàn)樵谟脩舻慕嵌?,加載完頁(yè)面時(shí)看到的獲得焦點(diǎn)的那個(gè)輸入框,它的提示文字是看不到的。

HTML:

<input type="text" id="uname" name="uname" placeholder="請(qǐng)輸入用戶名"/>

CSS:

.phcolor{ color:#999;}

JS:

$(function(){  

  //判斷瀏覽器是否支持placeholder屬性
  supportPlaceholder='placeholder'in document.createElement('input'),

  placeholder=function(input){

    var text = input.attr('placeholder'),
    defaultValue = input.defaultValue;

    if(!defaultValue){

      input.val(text).addClass("phcolor");
    }

    input.focus(function(){

      if(input.val() == text){
  
        $(this).val("");
      }
    });

 
    input.blur(function(){

      if(input.val() == ""){
      
        $(this).val(text).addClass("phcolor");
      }
    });

    //輸入的字符不為灰色
    input.keydown(function(){
 
      $(this).removeClass("phcolor");
    });
  };

  //當(dāng)瀏覽器不支持placeholder屬性時(shí),調(diào)用placeholder函數(shù)
  if(!supportPlaceholder){

    $('input').each(function(){

      text = $(this).attr("placeholder");

      if($(this).attr("type") == "text"){

        placeholder($(this));
      }
    });
  }

});

經(jīng)過(guò)測(cè)試可以達(dá)到IE11placeholder的顯示效果。

方法二

此方法的思路是做一張含有提示文字的圖片作為input輸入框的背景圖,初始時(shí)獲得焦點(diǎn)同時(shí)加載背景圖;

背景圖如下:

當(dāng)輸入框不為空時(shí),去掉背景圖;

當(dāng)輸入框?yàn)榭諘r(shí),加載背景圖;

當(dāng)用戶鍵盤(pán)按鍵且輸入框不為空( 輸入字符 )時(shí),去掉背景圖;

當(dāng)用戶鍵盤(pán)按鍵且輸入框?yàn)榭? 刪除字符 )時(shí),加載背景圖。

此方法的缺點(diǎn)是:需要為每一個(gè)提示文字不同的input制作背景圖片,并且設(shè)置input的樣式。

HTML代碼不變。

CSS:

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

JS:

$(function(){  

   //判斷瀏覽器是否支持placeholder屬性
   supportPlaceholder='placeholder' in document.createElement('input');

   if(!supportPlaceholder){

     //初始狀態(tài)添加背景圖片
     $("#uname").attr("class","phbg");
     //初始狀態(tài)獲得焦點(diǎn)
     $("#uname").focus;

     function destyle(){
     
      if($("#uname").val() != ""){
        
        $("#uname").removeClass("phbg");
      }else{
      
        $("#uname").attr("class","phbg");
       }
     }
     
     //當(dāng)輸入框?yàn)榭諘r(shí),添加背景圖片;有值時(shí)去掉背景圖片
     destyle();

     $("#uname").keyup(function(){

      //當(dāng)輸入框有按鍵輸入同時(shí)輸入框不為空時(shí),去掉背景圖片;有按鍵輸入同時(shí)為空時(shí)(刪除字符),添加背景圖片
      destyle();
     });

     $("#uname").keydown(function(){
     
      //keydown事件可以在按鍵按下的第一時(shí)間去掉背景圖片
      $("#uname").removeClass("phbg");
     });
   }
});

此方法至此結(jié)束。

此方法在IETester的IE8下顯示效果:

獲得焦點(diǎn)時(shí):

失去焦點(diǎn)時(shí):

有輸入時(shí):

如果有朋友有更好的方法,歡迎交流討論。

相關(guān)文章

最新評(píng)論