HTML DOM 標籤物件
HTML 中的 HTML DOM 標籤物件表示 <label> 元素。
語法
以下是語法 −
建立 a <label> 元素
var labelObject = document.createElement(“LABEL”)
屬性
在這裡,“標籤物件”可以有以下屬性 −
| 屬性 | 描述 |
|---|---|
| 控制元件 | 返回標籤控制元件 |
| 表單 | 返回包含標籤的包裹表單的引用 |
| htmlFor | 返回/設定標籤 for 屬性的值 |
示例
讓我們看一個關於 Label htmlFor屬性的示例 −
<!DOCTYPE html>
<html>
<head>
<title>Label htmlFor</title>
<style>
form {
width:70%;
margin: 0 auto;
text-align: center;
}
* {
padding: 2px;
margin:5px;
}
input[type="button"] {
border-radius: 10px;
}
</style>
</head>
<body>
<form>
<fieldset>
<legend>Label-htmlFor</legend>
<label id="CurrentEditor" for="editorTwo">Current Editor:</label><br>
<input type="text" id="editorOne" placeholder="editorOne">
<input type="text" id="editorTwo" placeholder="editorTwo">
<input type="button" onclick="getEventData()" value="Change Editor">
<div id="divDisplay">Label for attribute set as editor two</div>
</fieldset>
</form>
<script>
var divDisplay = document.getElementById("divDisplay");
var labelSelect = document.getElementById("CurrentEditor");
function getEventData() {
if(labelSelect.htmlFor === 'editorTwo'){
divDisplay.textContent = 'Label for attribute set as editor one';
labelSelect.htmlFor = 'editorOne';
}
}
</script>
</body>
</html>輸出
這將產生以下輸出 −
點選 “更改編輯器” 按鈕之前 −

點選 “更改編輯器” 按鈕之後 −

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