HTML 中的属性
HTML 中的属性将 <label> 元素绑定到第一个可标记元素,该元素的 id 与 for 属性的值相同。
语法
语法如下 −
1. 返回 for 属性的值 −
labelObject.htmlFor
示例
我们来看一个 for 属性的示例 −
<!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>将生成以下输出 −
1) 点击 “更改编辑器” 按钮之前 −

2) 点击 “更改编辑器” 按钮之后 −

广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
层叠样式表
Android
Python
C 编程
C++
C#
MongoDB
MySQL
JavaScript
PHP