kri*_*ish 2 html javascript jquery
我想输入类型更改为text从password当我点击"显示密码"复选框.
这就是我写的.这是对的吗?
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="password" class="password"/>
<input type="password" class="password"/>
<input type="checkbox" onchange="document.getElementsByClassName('password').type = this.checked ? 'text' : 'password'"> Show passwordRun Code Online (Sandbox Code Playgroud)
在纯JavaScript中,您可以这样做,
getElementsByClassName('password')将为您提供节点集合.您必须迭代它并将更改应用于每个.
forEach()无法应用于集合,因此Array.from(...)在此处使用从节点集合创建数组.
function doSomething(isChecked) {
var type = isChecked ? 'text' : 'password';
Array.from(document.getElementsByClassName('password')).forEach(element => {
element.type = type;
});
}Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="password" class="password"/>
<input type="password" class="password"/>
<input type="checkbox" onchange="doSomething(this.checked)"> Show passwordRun Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
85 次 |
| 最近记录: |