使用"for"属性动态创建标签标签

JoJ*_*oJo 3 javascript reserved-words prototypejs

在HTML中,您可以指定label标签的"for"属性,以便在用户单击标签时选择相应的单选按钮:

<input type="radio" name="group" value="1" id="radioButtonId" />
<label for="radioButtonId">label text</label>
Run Code Online (Sandbox Code Playgroud)

使用javascript动态创建标签标签时存在问题(特别是使用Prototype JS框架).Forfor循环的保留关键字.原型JS的文档显示,类名是保留的关键字码字,但它没有说的码字什么是.它是什么?

new Element(
 'label', {
  for: 'radioButtonId'
 }
).update('label text');
Run Code Online (Sandbox Code Playgroud)

bob*_*nce 9

className是与属性对应的标准DOM class属性; 它与Prototype本身无关.

类似地,对应于for属性的DOM 属性是htmlFor.


Nic*_*ver 5

要将保留的工作用作对象文字中的键,只需将其包装在引号中,如下所示:

new Element(
 'label', {
  'for': 'radioButtonId'
 }
).update('label text');
Run Code Online (Sandbox Code Playgroud)

  • +1或者你可以使用属性名称`htmlFor`. (3认同)