欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

jquery搜索框效果實(shí)現(xiàn)方法

 更新時(shí)間:2015年01月16日 11:35:13   投稿:shichen2014  
這篇文章主要介紹了jquery搜索框效果實(shí)現(xiàn)方法,分析了jquery搜索框效果的實(shí)現(xiàn)技巧及注意事項(xiàng),具有一定參考借鑒價(jià)值,需要的朋友可以參考下

本文實(shí)例講述了jquery搜索框效果實(shí)現(xiàn)方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:

復(fù)制代碼 代碼如下:
<html>
<head>
<title>jquery:搜索框效果</title>
<script type="text/javascript" src="jquery-1.8.2.min.js"></script>
<script type="text/javascript">
 $(function(){
  $('#search').val("請(qǐng)輸入搜索內(nèi)容").addClass("c1");
  $('#search').focus(function(){//搜索框獲得焦點(diǎn)時(shí)
   $('#search').val("").addClass("c2");
  });
  $('#search').blur(function(){//搜索框失去焦點(diǎn)時(shí)
   if($('#search').val()==""){
    $('#search').val("請(qǐng)輸入搜索內(nèi)容").attr("class","c1");
   }
  });
 });
</script>
<style type="text/css">
.c1{color:gray;font-style:italic;}
.c2{color:#000;font-style:normal;}
</style>
</head>
<body>
<input type="text" size="38" id="search" /><button>搜索</button>
</body>
</html>

補(bǔ)充說(shuō)明:有些不完美,如果搜索框原來(lái)還有其它樣式,當(dāng)失去焦點(diǎn)時(shí),如果采用例子中的代碼,那其它樣式也會(huì)沒(méi)了,因?yàn)閍ttr()為設(shè)置樣式。如果采用addClass()為追加樣式,也不怎么合適,雖然能達(dá)到效果,但原來(lái)的c2樣式還在,顯示時(shí)被c1樣式替換而已(這需要c1樣式寫在c2后面)。好像沒(méi)有替換樣式的方法?

修改后解決上面的問(wèn)題,較完美版本(代碼還可以優(yōu)化更簡(jiǎn)單)

復(fù)制代碼 代碼如下:
<html>
<head>
<title>jquery:搜索框效果</title>
<script type="text/javascript" src="jquery-1.8.2.min.js"></script>
<script type="text/javascript">
 $(function(){
  $('#search').val("請(qǐng)輸入搜索內(nèi)容").addClass("c1");
  $('#search').focus(function(){//搜索框獲得焦點(diǎn)時(shí)
   if($('#search').val()=="請(qǐng)輸入搜索內(nèi)容"){
    $('#search').val("").addClass("c2").removeClass("c1");
   }
  });
  $('#search').blur(function(){//搜索框失去焦點(diǎn)時(shí)
   if($('#search').val()==""){
    $('#search').val("請(qǐng)輸入搜索內(nèi)容").addClass("c1").removeClass("c2");
   }
  });
 });
</script>
<style type="text/css">
.c1{color:gray;font-style:italic;}
.c2{color:#000;font-style:normal;}
</style>
</head>
<body>
<input type="text" size="38" id="search" /><button>搜索</button>
</body>
</html>

希望本文所述對(duì)大家的jQuery程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論