這篇文章主要介紹了實(shí)現(xiàn)密碼框(password)中顯示文字提示功能代碼,在項(xiàng)目開(kāi)發(fā)中經(jīng)常會(huì)用到,需要的朋友可以參考下
其實(shí)實(shí)際上實(shí)現(xiàn)中并不能讓password中顯示文字提示,但是我們?cè)诠ぷ髦杏羞@樣的需求,當(dāng)沒(méi)輸入東西的時(shí)候,框內(nèi)有提示輸入密碼,但是當(dāng)輸入東西的時(shí)候又顯示的是*號(hào),那么是如何實(shí)現(xiàn)的呢?其實(shí)原理很簡(jiǎn)單,就是放兩個(gè)文本框,樣式以及定位都是一樣的。先將type為password的隱藏,只顯示type為text的偽密碼框,value設(shè)置提示內(nèi)容例如請(qǐng)輸入密碼。然后當(dāng)input觸發(fā)的時(shí)候,type為text的input隱藏,讓type為password的input顯示出來(lái)。然后當(dāng)檢測(cè)password的value為空的時(shí)候,再將type為password隱藏,type為text的顯示。啥話也不說(shuō)了,上代碼。(ps:額外添加了重置功能)
先上html部分
<table class="login_table">
<tr>
<td>賬號(hào)</td>
<td><input type="text" value="請(qǐng)輸入您的賬號(hào)" class="admin" /></td>
</tr>
<tr>
<td>密碼</td>
<td>
<input type="text" value="請(qǐng)輸入您的密碼"id="login_showPwd" />
<input type="password"id="login_password" style="display: none"/>
</td>
</tr>
<tr>
<td>
<input type="button" value="登錄" />
<input type="button" value="重置" id ="btn_res"/>
</td>
</tr>
</table>
然后上js部分
//賬號(hào)部分
$(function(){
$(".admin").focus(function(){
if($(this).val() == "請(qǐng)輸入您的賬號(hào)"){
$(this).val("");
}
});
$(".admin").blur(function(){
if($(this).val() == ""){
$(this).val("請(qǐng)輸入您的賬號(hào)");
}
});
// 密碼部分
$('#login_showPwd').focus(function(){
var text_value=$(this).val();
if(text_value == this.defaultValue){
$('#login_showPwd').hide();
$('#login_password').show().focus();
}
});
$('#login_password').blur(function(){
var text_value = $(this).val();
if(text_value==""){
$('#login_showPwd').show();
$('#login_password').hide();
}
});
//重置部分
$('#btn_res').click(function(){
$('.admin').val("請(qǐng)輸入您的賬號(hào)");
$('#login_password').hide().val("");
$("#login_showPwd").show();
});
});
下面給大家介紹密碼輸入框 底下顯示的文字方法
一個(gè)小的提示很多網(wǎng)站密碼輸入框里面都有密碼兩個(gè)字,以前以為是text的,值到今天才知道,它就是password標(biāo)簽,寫法如下
<input type="password" name="pas" placeholder="密碼"/>
加了一個(gè)placeholder屬性就好了
以上所述是小編給大家介紹的實(shí)現(xiàn)密碼框(password)中顯示文字提示功能代碼的相關(guān)知識(shí),希望對(duì)大家有所幫助