是否有关于标签和输入HTML元素嵌套的最佳实践?
经典的方式:
<label for="myinput">My Text</label>
<input type="text" id="myinput" />
or
<label for="myinput">My Text
<input type="text" id="myinput" />
</label>
是否有关于标签和输入HTML元素嵌套的最佳实践?
经典的方式:
<label for="myinput">My Text</label>
<input type="text" id="myinput" />
or
<label for="myinput">My Text
<input type="text" id="myinput" />
</label>
当前回答
我更喜欢
<label>
Firstname
<input name="firstname" />
</label>
<label>
Lastname
<input name="lastname" />
</label>
over
<label for="firstname">Firstname</label>
<input name="firstname" id="firstname" />
<label for="lastname">Lastname</label>
<input name="lastname" id="lastname" />
主要是因为它使HTML更具可读性。实际上,我认为我的第一个例子更容易用CSS来设置样式,因为CSS可以很好地处理嵌套元素。
但我想这只是个人品味的问题。
如果需要更多样式选项,请添加span标记。
<label>
<span>Firstname</span>
<input name="firstname" />
</label>
<label>
<span>Lastname</span>
<input name="lastname" />
</label>
在我看来,代码看起来还是更好。
其他回答
就个人而言,我喜欢把标签放在外面,就像你的第二个例子一样。这就是FOR属性存在的原因。原因是我经常会在标签上应用样式,比如宽度,以使表单看起来更好(下面是简写):
<style>
label {
width: 120px;
margin-right: 10px;
}
</style>
<label for="myinput">My Text</label>
<input type="text" id="myinput" /><br />
<label for="myinput2">My Text2</label>
<input type="text" id="myinput2" />
这样我就可以避免表格和表单中的所有垃圾。
行为差异:点击标签和输入之间的空格
如果你点击标签和输入之间的空格,它只在标签包含输入时激活输入。
这是有意义的,因为在这种情况下,空格只是标签的另一个字符。
div { 边框:1px纯黑色; } 标签{ 边框:1px纯黑色; 填充:5 px; } 输入{ margin-right: 30 px; } 内< p >: < / p > <标识> <input type="checkbox" /> 标签。在我和复选框之间单击。 < / >标签 外的< p >: < / p > <input type="checkbox" id="check" /> <标签= >“查看”标签。</label> .在我和复选框之间单击
能够在标签和方框之间单击意味着它是:
更容易点击 不太清楚事情的开始和结束
引导复选框v3.3示例使用了里面的输入:http://getbootstrap.com/css/#forms遵循它们可能是明智的。但是他们在4.0版本改变了主意https://getbootstrap.com/docs/4.0/components/forms/#checkboxes-and-radios所以我不知道什么是明智的了:
复选框和无线电的使用是为支持基于html的表单验证而构建的,并提供简洁、可访问的标签。因此,我们的<input>s和<label>s是兄弟元素,而不是<label>中的<input>。这稍微有点冗长,因为您必须指定id和属性来关联<input>和<label>。
详细讨论这一点的UX问题:https://ux.stackexchange.com/questions/23552/should-the-space-between-the-checkbox-and-label-be-clickable
您需要考虑的一件事是复选框和无线电输入与javascript的交互。
使用下面的结构:
<label>
<input onclick="controlCheckbox()" type="checkbox" checked="checkboxState" />
<span>Label text</span>
</label>
当用户点击“标签文本”时,controlCheckbox()函数将被触发一次。
但是,当输入标记被单击时,controlCheckbox()函数可能会在一些较老的浏览器中被触发两次。这是因为输入和标签标签都会触发附加到复选框的onclick事件。
然后您的checkboxState中可能会有一些错误。
我最近在IE11上遇到了这个问题。我不确定现代浏览器是否有这种结构的问题。
As most people have said, both ways work indeed, but I think only the first one should. Being semantically strict, the label does not "contain" the input. In my opinion, containment (parent/child) relationship in the markup structure should reflect containment in the visual output. i.e., an element surrounding another one in the markup should be drawn around that one in the browser. According to this, the label should be the input's sibling, not it's parent. So option number two is arbitrary and confusing. Everyone that has read the Zen of Python will probably agree (Flat is better than nested, Sparse is better than dense, There should be one-- and preferably only one --obvious way to do it...).
因为W3C和主要浏览器供应商的决定(允许“任何你喜欢的方式来做”,而不是“做正确的方式”),今天的网络是如此混乱,我们开发人员不得不处理如此复杂和多样化的遗留代码。
如果在label标签中包含input标签,则不需要使用'for'属性。
也就是说,我不喜欢在标签中包含input标签,因为我认为它们是独立的,不包含实体。