HTML DOM Input Checkbox defaultChecked 属性
HTML DOM input checkbox defaultChecked 属性会返还 HTML 复选框中 checked 特性的默认值。
语法
以下为语法 −
object.defaultChecked
示例
我们来看一个 defaultChecked 属性的示例 −
<!DOCTYPE html>
<html>
<head>
<title>HTML DOM checked property</title>
<style>
body{
text-align:center;
}
p{
font-size:1.5rem;
color:#ff8741;
}
input{
width:30px;
height:30px;
}
button{
background-color:#db133a;
color:#fff;
padding:8px;
border:none;
width:180px;
margin:0.5rem;
border-radius:50px;
outline:none;
font-weight:bold;
}
.show-msg{
color:#db133a;
font-size:1.5rem;
font-weight:bold;
}
</style>
</head>
<body>
<h1>checked Property Example</h1>
<p>Are you smart?</p>
<input type="checkbox" checked>
<br>
<button onclick="check()">Lets Check? Click me</button>
<div class="show-msg"></div>
<script>
function check() {
var input = document.querySelector("input");
var showMsg = document.querySelector(".show-msg");
if(input.defaultChecked === true){
showMsg.innerHTML="You are right!You are smart by default";
} else {
showMsg.innerHTML="Hmmmmm!You are not smart by default";
}
}
</script>
</body>
</html>输出
这会产生以下输出 −

单击“我们检查一下?点击我”按钮以检查该复选框是否由默认勾选。

广告
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 程序设计
C++
C#
MongoDB
MySQL
Javascript
PHP