js動(dòng)態(tài)修改input輸入框的type屬性(實(shí)現(xiàn)方法解析)
需要實(shí)現(xiàn)的效果:一個(gè)輸入框,當(dāng)輸入框未獲得焦點(diǎn)的時(shí)候,value 值為 “密碼”;當(dāng)輸入框失去焦點(diǎn)的時(shí)候,輸入內(nèi)容顯示為”*****”
<input name=”password” type=”text” id=”showPwd” tabindex=”2″ class=”input” value=”密碼” />
我們很直接會(huì)想到下面的js
$(“#showPwd”).focus(function(){
$(this).attr(‘type','password');
});
發(fā)現(xiàn)并沒(méi)有實(shí)現(xiàn)預(yù)期效果,出現(xiàn) uncaught exception type property can't be changed 錯(cuò)誤,查看jQuery 1.42源碼 1488 行
// We can't allow the type property to be changed (since it causes problems in IE)
if ( name === “type” && rtype.test( elem.nodeName ) && elem.parentNode ) {
jQuery.error( “type property can't be changed” );
}
jQuery 修改不了用源生的JS呢?
$(“#pwd”).focus(function(){
$(“#pwd”)[0].type = ‘password';
$(“#pwd”).val(“”);
});
發(fā)現(xiàn)在FF下可以修改并將密碼輸入框type 修改為 “password” 并將 value設(shè)置為空,而IE下卻提示無(wú)法得到type屬性,不支持該命令。 彈出 type 看看真的無(wú)法得到嗎?
$(“#showPwd”).focus(function(){
alert($(“#showPwd”)[0].type);
$(“#showPwd”)[0].type = ‘password';
$(“#showPwd”).val(“”);
});
發(fā)現(xiàn)彈出text ,原來(lái)不是無(wú)法得到,只是IE下不能修改。 因此,我們想到可以先remove然后再生成一個(gè)type是password的密碼輸入框。
下面type為password的輸入框
<input name=”password” type=”password” id=”password” class=”input” style=”display: none;” />
$(“#showPwd”).focus(function() {
var text_value = $(this).val();
if (text_value == this.defaultValue) {
$(“#showPwd”).hide();
$(“#password”).show().focus();
}
});
$(“#password”).blur(function() {
var text_value = $(this).val();
if (text_value == “”) {
$(“#showPwd”).show();
$(“#password”).hide();
}
});
最終效果: 當(dāng)輸入框獲得焦點(diǎn)的時(shí),輸入的內(nèi)容顯示為“****”;當(dāng)失去焦點(diǎn)的時(shí),內(nèi)容為空時(shí)顯示“密碼”。
- Vue.js 帶下拉選項(xiàng)的輸入框(Textbox with Dropdown)組件
- Vue.js數(shù)字輸入框組件使用方法詳解
- JS 仿支付寶input文本輸入框放大組件的實(shí)例
- JavaScript組件開(kāi)發(fā)之輸入框加候選框
- JS通過(guò)正則限制 input 輸入框只能輸入整數(shù)、小數(shù)(金額或者現(xiàn)金) 兩位小數(shù)
- js與jquery實(shí)時(shí)監(jiān)聽(tīng)輸入框值的oninput與onpropertychange方法
- js監(jiān)聽(tīng)輸入框值的即時(shí)變化onpropertychange、oninput
- JS實(shí)現(xiàn)在網(wǎng)頁(yè)中彈出一個(gè)輸入框的方法
- js監(jiān)聽(tīng)input輸入框值的實(shí)時(shí)變化實(shí)例
- JavaScript實(shí)現(xiàn)一個(gè)輸入框組件
相關(guān)文章
js實(shí)現(xiàn)的捐贈(zèng)管理完整實(shí)例
這篇文章主要介紹了js實(shí)現(xiàn)的捐贈(zèng)管理完整實(shí)例,包括了html頁(yè)面、js腳本及css樣式的完整實(shí)現(xiàn)代碼,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-01-01
bootstrap動(dòng)態(tài)添加面包屑(breadcrumb)及其響應(yīng)事件的方法
這篇文章主要介紹了bootstrap動(dòng)態(tài)添加面包屑(breadcrumb)及其響應(yīng)事件的方法,涉及js數(shù)據(jù)傳輸及定義響應(yīng)事件相關(guān)操作技巧,需要的朋友可以參考下2017-05-05
JavaScript實(shí)現(xiàn)文本相似度對(duì)比
這篇文章主要介紹了JavaScript實(shí)現(xiàn)文本相似度對(duì)比,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-06-06
JavaScript實(shí)現(xiàn)簡(jiǎn)單進(jìn)度條效果
這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)簡(jiǎn)單進(jìn)度條效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-03-03
javascript 設(shè)置文本框中焦點(diǎn)的位置
設(shè)置文本框中焦點(diǎn)的位置的實(shí)現(xiàn)代碼2009-11-11
如何使用Bootstrap的modal組件自定義alert,confirm和modal對(duì)話框
本文我將為大家介紹Bootstrap中的彈出窗口組件Modal,此組件簡(jiǎn)單易用,效果大氣漂亮且很實(shí)用,感興趣的朋友一起學(xué)習(xí)吧2016-03-03

