Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Css 为什么我需要用标签绑定复选框?_Css_Checkbox_Label_For Attribute - Fatal编程技术网

Css 为什么我需要用标签绑定复选框?

Css 为什么我需要用标签绑定复选框?,css,checkbox,label,for-attribute,Css,Checkbox,Label,For Attribute,我这里有一个简短的问题。为什么我们总是将复选框id绑定到标签中的for属性?它有什么目的吗?如果是的话,你能把它列出来吗?以及它的技术优势 提前感谢,, Madhu.您不必总是将复选框(或任何输入)id绑定到标签。它允许您单击标签以触发复选框(或输入)的单击。如果label元素包装,而不需要id/for,则此功能也有效: <label for="checkbox">Click checkbox</label> <input id="checkbox" type=ch

我这里有一个简短的问题。为什么我们总是将复选框id绑定到标签中的for属性?它有什么目的吗?如果是的话,你能把它列出来吗?以及它的技术优势

提前感谢,, Madhu.

您不必总是将复选框(或任何输入)id绑定到标签。它允许您单击标签以触发复选框(或输入)的单击。如果label元素包装
,而不需要id/for,则此功能也有效:

<label for="checkbox">Click checkbox</label> <input id="checkbox" type=checkbox>
<label>Click checkbox <input type=checkbox></label>
单击复选框
单击复选框

是的,它确实有目的。基本上,它允许你“点击”它,它也有用于屏幕阅读器


回答:

根据可访问性指南,您必须始终提供一个可编程确定的标签。但是将
输入
元素包装在
标签
中就足够了。只是有点不自然。@JukkaK.Korpela为什么不自然?输入控件在逻辑上不是其标签的一部分。ExplosionPills和Jukka:非常感谢您的回复。这就是我想要的答案:)