小编JTr*_*x16的帖子

HTML5属性操作:Vanilla JS与jQuery

为什么在vanilla JS和jQuery中更改元素的属性值有时不会起作用?一种情况是我创建的实验,无论何时单击"显示密码"按钮,密码字段的type属性值都应该更改为何textpassword,反之亦然.它在vanilla JS中成功运行,但在jQuery中却没有.在jQuery中,属性值会更改为text,但是当我想将其更改回时password,它将无法工作.为什么会出现这样的问题?

香草JS

document.querySelector('.show-password').onclick = function() {
    if(document.querySelector('#password').type == 'password') {
        document.querySelector('#password').type = 'text';
    } else {
        document.querySelector('#password').type = 'password';
    }
}
Run Code Online (Sandbox Code Playgroud)

jQuery的

$('.show-password').click(function() {
    if($('#password').attr('type', 'password')) {
        $('#password').attr('type', 'text');
    } else {
        $('#password').attr('type', 'password');
    }
});
Run Code Online (Sandbox Code Playgroud)

javascript jquery html5 attributes input

0
推荐指数
1
解决办法
91
查看次数

标签 统计

attributes ×1

html5 ×1

input ×1

javascript ×1

jquery ×1