HTML DOM Label htmlFor คุณสมบัติส่งกลับ/ตั้งค่าสำหรับแอตทริบิวต์
ไวยากรณ์
ต่อไปนี้เป็นไวยากรณ์ -
ส่งคืนค่า สำหรับ แอตทริบิวต์ −
labelObject.htmlFor
ตัวอย่าง
ให้เราดูตัวอย่างสำหรับ 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> ผลลัพธ์
สิ่งนี้จะสร้างผลลัพธ์ต่อไปนี้ -
ก่อนคลิก 'เปลี่ยนตัวแก้ไข' ปุ่ม −

หลังจากคลิก 'เปลี่ยนตัวแก้ไข' ปุ่ม −
