在 JavaScript 中设置 HTML <label> 'for' 属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15750290/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 01:58:22 来源:igfitidea点击:
Setting the HTML <label> 'for' attribute in JavaScript
提问by Drew Noakes
How do you set the for
attribute of an HTML <label>
element in JavaScript, without using jQuery or any other library?
如何在 JavaScript 中设置for
HTML<label>
元素的属性,而不使用 jQuery 或任何其他库?
回答by Drew Noakes
Use the htmlFor
attribute. I assume it has a slightly cryptic name because for
is a keyword in JavaScript:
使用htmlFor
属性。我认为它的名称有点神秘,因为它for
是 JavaScript 中的一个关键字:
var label = document.createElement('label');
label.htmlFor = 'some-input-id';