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>

輸出

這將產生以下輸出 −

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

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

更新日期:2019 年 7 月 30 日

182 次瀏覽

開啟您的 職業生涯

完成課程,獲得認證

立即開始
廣告
© . All rights reserved.