国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

js動態修改input輸入框的type屬性(實現方法解析)

2024-05-06 15:54:54
字體:
來源:轉載
供稿:網友
本文是對js動態修改input輸入框的type屬性的實現方法。進行了詳細的介紹,需要的朋友可以過來參考下,希望對大家有所幫助

需要實現的效果:一個輸入框,當輸入框未獲得焦點的時候,value 值為 “密碼”;當輸入框失去焦點的時候,輸入內容顯示為”*****”

<input name=”password” type=”text” id=”showPwd” tabindex=”2″ class=”input” value=”密碼” />

我們很直接會想到下面的js

$(“#showPwd”).focus(function(){
$(this).attr(‘type','password');
});

發現并沒有實現預期效果,出現 uncaught exception type property can't be changed 錯誤,查看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(“”);
});

發現在FF下可以修改并將密碼輸入框type 修改為 “password” 并將 value設置為空,而IE下卻提示無法得到type屬性,不支持該命令。 彈出 type 看看真的無法得到嗎?

$(“#showPwd”).focus(function(){
alert($(“#showPwd”)[0].type);
$(“#showPwd”)[0].type = ‘password';
$(“#showPwd”).val(“”);
});

發現彈出text ,原來不是無法得到,只是IE下不能修改。 因此,我們想到可以先remove然后再生成一個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();
}
});

最終效果: 當輸入框獲得焦點的時,輸入的內容顯示為“****”;當失去焦點的時,內容為空時顯示“密碼”。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 航空| 壤塘县| 沧州市| 鹤岗市| 大新县| 泸定县| 佛坪县| 宁阳县| 澳门| 杨浦区| 武定县| 舞钢市| 晴隆县| 巴南区| 咸丰县| 登封市| 绵竹市| 宜丰县| 保德县| 勐海县| 永清县| 大田县| 康定县| 祥云县| 富源县| 岢岚县| 卢龙县| 德惠市| 明水县| 宿松县| 丰镇市| 高安市| 肇东市| 辽阳县| 股票| 黎平县| 巴彦淖尔市| 营口市| 扶余县| 莱州市| 洪湖市|