关于 HTML 标签的“For"财产

2022-01-25 00:00:00 label html

考虑以下 2 行代码(复制自 w3schools.com > "HTML < label > for Attribute"):

Considering the following 2 lines of code (copied from w3schools.com > "HTML < label > for Attribute"):

  <label for="male">Male </label>
  <input type="radio" name="sex" id="male" />

我无法发现上述标签的for"属性的确切用途.如您所见,它当前设置为男性"(以匹配输入控件的 id).

I am having trouble discovering the exact purpose of the above label's "for" property. As you can see it is currently set to "male" (to match the id of the input control).

到目前为止,我所读到的只是上面的代码将标签与输入控件关联"和绑定".所以我的问题是,这到底是什么意思?

All I have read so far is that the above code will 'associate' and 'bound' the label with the input control. So my question is, what exactly does this mean?

将标签关联到输入控件的具体结果是什么?
标签和/或输入是否由于这种关联"而具有新的行为?

What exactly are the results of associating the label to the input control?
Does the label and/or input have new behaviours as a result of this 'association'?

推荐答案

通过 for 与控件关联的 label 将是可点击的.单击它选择控件.特别是对单选/复选框非常有用.它还对视障人士的屏幕阅读器的可访问性产生了影响.

A label that is associated with a control via for will be clickable. Clicking it selects the control. Highly useful with radio/checkboxes in particular. It also has accessibility implications for screen readers for the visually impaired.

相关文章