我想使用“自标记”输入字段来接受带有 JavaScript 的用户名和密码,如下所示。从用户体验 Angular 来看,它似乎运作良好。然而,由于我在焦点/模糊时更改密码输入字段的“类型”,我担心这是否会带来任何风险。它可能不应该,但只是想确定一下。
<form method="post" action="/login">
<fieldset>
<h3>Login</h3>
<input id="username" name="username" type="text" value="Username" onblur="if (this.value == '') { this.value = 'Username'; }" onfocus="if (this.value == 'Username') { this.value = ''; }"/>
<input id="password" name="password" type="text" value="Password " onblur="if (this.value == '') { this.value = 'Password '; this.type = 'text' }" onfocus="this.type = 'password'; if (this.value == 'Password ') { this.value = ''; }"/>
<input id="submit" name="submit" type="submit" value="Login"/>
</fieldset>
</form>
最佳答案
要执行您想要的操作,请使用输入字段占位符:
<input type="password" placeholder="Password">
它将在所有现代浏览器中正常工作。
关于javascript - 以下使用 JavaScript 处理 html <input> 是否会带来任何风险?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18023801/