<html>
<body>
<form action="/demo/htmldom/action_page.php">
密码:<input type="password" id="myPsw" name="usr_psw">
<input type="submit">
</form>
<p>单击“试一试”按钮可为密码字段设置 required 属性。</p>
<p><b>提示:</b>请在点击“试一试”按钮前后点击“提交”按钮,查看效果。</p>
<button onclick="myFunction()">试一试</button>
<p id="demo"></p>
<script>
function myFunction() {
document.getElementById("myPsw").required = true;
document.getElementById("demo").innerHTML = "required 属性已设置。现在必须在提交表单之前填写密码字段。";
}
</script>
</body>
</html>