我們都知道HTML5的input新屬性有 placeholder="",那么這個(gè)不兼容IE低版本我們只能用腳本來寫了。
首先HTML新建一個(gè)input
<input type="text" class="input" value="請(qǐng)輸入搜索內(nèi)容" />
然后我們?cè)僖胂鄳?yīng)的js庫(kù),再使用jQuery
<script src="js/jquery-1.8.3.min.js"></script> <script> $(".input").bind({ focus:function(){ if (this.value == this.defaultValue){ this.value=""; } }, blur:function(){ if (this.value == ""){ this.value = this.defaultValue; } } }); </script>簡(jiǎn)單吧,這樣就可以了,那么這個(gè)是input的屬性是text,我們要密碼password也一樣可以顯示IE低版本,我們用的方法就是用兩個(gè)input,一個(gè)text默認(rèn)顯示,一個(gè)password默認(rèn)隱藏,當(dāng)text獲取焦點(diǎn)時(shí)password顯示,text隱藏,沒有輸入內(nèi)容失去焦點(diǎn)text顯示,password顯示,好了,廢話不多說,看代碼!
<!doctype html><html lang="en"><head> <meta charset="UTF-8" /> <title>密碼框提示</title></head><body> <input type="text" value="登錄密碼" class="show" > <input type="password" class="log_paw" style="display: none;"> <script src="js/jquery-1.8.3.min.js"></script> <script> $('.show').focus(function(){ var text_value = $(this).val(); if (text_value == this.defaultValue) { $(this).hide(); $(this).next('input.log_paw').show().focus(); } }); $('input.log_paw').blur(function() { var text_value = $(this).val(); if (text_value == '') { $(this).prev('.show').show(); $(this).hide(); } }); </script></body></html>好了,代碼就在這里了,希望能幫助到你!
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持錯(cuò)新站長(zhǎng)站。
新聞熱點(diǎn)
疑難解答
圖片精選