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