<label> 中的 HTML 标签
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4461942/
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
HTML tags inside <label>
提问by SaltyNuts
I have a table in a page that consists of checkboxes in the cells on the left and descriptions in the cells on the right. The "description" contains h4 headers and plain text. I want to make that whole description (everything inside <td></td>
) a label.
我在页面中有一个表格,它由左侧单元格中的复选框和右侧单元格中的描述组成。“描述”包含 h4 标题和纯文本。我想让整个描述(里面的所有内容<td></td>
)成为一个标签。
So each row looks like this:
所以每一行看起来像这样:
<tr><td><input type="checkbox" name="entiries[]" value="i1" id="i1"></td>
<td><label for="i1">
<h4>Some stuff</h4>more stuff..
<h4>Some stuff</h4>more stuff..
</label>
</td></tr>
This does not work however, the text does not act like a label and is not clickable. I'm using Firefox 3.6 to test it. If I remove <h4>
tags it starts working, but that complicates formatting. Is there something about <h*>
tags that prevents <label>
from working correctly?
但是,这不起作用,文本不像标签,并且不可点击。我正在使用 Firefox 3.6 来测试它。如果我删除<h4>
标签,它就会开始工作,但这会使格式复杂化。<h*>
标签是否存在妨碍<label>
正常工作的问题?
采纳答案by Tatu Ulmanen
Block level elements (to which h4
belongs) are not allowed inside inline elements, and will cause undefined behaviour. You can use span
elements instead.
h4
内联元素中不允许使用块级元素(属于哪个),并且会导致未定义的行为。您可以改用span
元素。
回答by Quentin
Only inline elements(except other label elements) may appear inside label elements.
只有内联元素(其他标签元素除外)可以出现在标签元素内。
<!ELEMENT LABEL - - (%inline;)* -(LABEL) -- form field label text -->
— http://www.w3.org/TR/html4/interact/forms.html#h-17.9.1
— http://www.w3.org/TR/html4/interact/forms.html#h-17.9.1
It doesn't make sense to put headings there anyway.
无论如何,将标题放在那里是没有意义的。
回答by Jamie Dixon
The <label>
element in HTML is an inline level element and cannot contain block level elements.
<label>
HTML 中的元素是内联级元素,不能包含块级元素。
This is probably what's causing your issues. Alternatively you can put your labels inside the <h4>
's :
这可能是导致您出现问题的原因。或者,您可以将标签放在<h4>
's 中:
<tr><td><input type="checkbox" name="entiries[]" value="i1" id="i1"></td>
<td><
<h4><label for="i1">Some stuff</label></h4>more stuff..
<h4><label for="i1">Some stuff</label></h4>more stuff..
</label>
</td></tr>