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

首頁 > 編程 > JavaScript > 正文

js動態(tài)修改input輸入框的type屬性(實(shí)現(xiàn)方法解析)

2019-11-20 21:43:14
字體:
供稿:網(wǎng)友

需要實(shí)現(xiàn)的效果:一個輸入框,當(dāng)輸入框未獲得焦點(diǎn)的時候,value 值為 “密碼”;當(dāng)輸入框失去焦點(diǎn)的時候,輸入內(nèi)容顯示為”*****”

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

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

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

發(fā)現(xiàn)并沒有實(shí)現(xiàn)預(yù)期效果,出現(xiàn) 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(“”);
});

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

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

發(fā)現(xiàn)彈出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();
}
});

最終效果: 當(dāng)輸入框獲得焦點(diǎn)的時,輸入的內(nèi)容顯示為“****”;當(dāng)失去焦點(diǎn)的時,內(nèi)容為空時顯示“密碼”。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 泸州市| 铁岭县| 瓮安县| 班玛县| 江源县| 商洛市| 平乐县| 周口市| 连州市| 梁河县| 富宁县| 江安县| 息烽县| 枣阳市| 新安县| 浙江省| 五寨县| 朝阳区| 荔浦县| 客服| 阳朔县| 新竹市| 方城县| 云龙县| 三门县| 碌曲县| 大兴区| 崇文区| 五指山市| 星座| 门头沟区| 阿鲁科尔沁旗| 应用必备| 铜梁县| 惠水县| 肃宁县| 蚌埠市| 惠东县| 阳江市| 芦溪县| 巴东县|