DOM标签htmlFor属性用于设置或返回元素的for属性的值。 For属性定义将标记哪个表单元素。
用法:
它用于返回htmlFor属性:labelObject.htmlFor
它用于设置htmlFor属性:labelObject.htmlFor = id
属性值:
id:它定义了应标记元素的id属性。
返回值:它返回一个字符串值,该值表示将被标记的元素的ID。
示例1:演示如何返回属性的HTML程序。
DOM Label htmlFor Propertybody {
font-size:20px;
}
GeeksforGeeks
DOM Label htmlfor Property
id="GFG">
Student
name="Occupation"
id="student"
value="student">
Business
name="Occupation"
id="business"
value="business">
Other
name="Occupation"
id="other"
value="other">
Submit
function myGeeks() {
// Return htmlfor property.
var g =
document.getElementById("GFG").htmlFor;
document.getElementById("sudo").innerHTML =
g;
}
输出:
在单击按钮之前:
单击按钮后:
示例2:演示如何设置htmlFor属性的HTML程序。
DOM Label htmlFor Propertybody {
font-size:20px;
}
GeeksforGeeks
DOM Label htmlfor Property
id="GFG">
Student
name="Occupation"
id="student"
value="student">
Business
name="Occupation"
id="business"
value="business">
Other
name="Occupation"
id="other"
value="other">
Submit
function myGeeks() {
// Set htmlfor property.
var g =
document.getElementById("GFG").htmlFor =
"Employes"
document.getElementById("sudo").innerHTML =
"The value of the For attribute was "
"changed from student to " + g;
}
输出:
在单击按钮之前:
单击按钮后:
支持的浏览器:下面列出了DOM标签htmlFor Property支持的浏览器:
谷歌浏览器
IE浏览器
Firefox
Opera
Safari