HTML DOM 輸入按鈕 form 屬性
HTML DOM 輸入按鈕 form 屬性返回包含輸入按鈕的表單的引用。
語法
以下為語法 −
object.form
示例
以下為 input 按鈕 form 屬性的示例 −
<!DOCTYPE html>
<html>
<head>
<title<HTML DOM form Property</title<
<style>
body{
text-align:center;
}
.btn{
display:block;
margin:1rem auto;
background-color:#db133a;
color:#fff;
border:1px solid #db133a;
padding:0.5rem;
border-radius:50px;
width:20%;
}
.show-message{
font-weight:bold;
font-size:1.4rem;
color:#ffc107;
}
</style>
</head>
<body>
<h1>form Property Example</h1>
<form id="form1">
<fieldset>
<legend>Form 1</legend>
<input type="button" class="btn" value="button 1">
<input type="button" class="btn" value="button 2">
</fieldset>
</form<
<form id="form2">
<fieldset>
<legend>Form 2</legend>
<input type="button" class="btn" value="button 1">
<input type="button" class="btn" value="button 2">
</fieldset>
</form>
<div class="show-message"></div>
<script>
var btnArr= document.querySelectorAll(".btn");
var showMessage= document.querySelector(".show-message");
btnArr.forEach((ele)=>{
ele.addEventListener("click",(e)=>{
showMessage.innerHTML="";
if(e.target.form.id === 'form1'){
showMessage.innerHTML="I'm from form 1";
} else {
showMessage.innerHTML="I'm from form 2";
}
})
});
</script>
</body>
</html>輸出
將生成以下輸出 −

單擊表單 1 的“按鈕 1/按鈕 2” −

現在,單擊表單 2 的“按鈕 1/按鈕 2” −

廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP