关闭HTML< input>标签问题 [英] Closing HTML <input> tag issue
问题描述
我想知道为什么HTML <input>
标签没有像其他HTML标签那样获得结束标签,如果我们关闭输入标签会出现什么问题呢?
I wonder why does the HTML <input>
tags don't get a closing tag like other HTML tags and what would go wrong if we do close input tag ?
我尝试使用Google,但发现编写这样的输入标签的标准<input type="text" name="name">
而不用</input>
将其关闭.
I tried to Google and I found the standard to write a input tag like this <input type="text" name="name">
not closing it with a </input>
.
当我使用
为Radio
按钮创建输入标签时,我个人感到了问题.
I personally felt the problem when I created a input tag for Radio
buttons using
var DOM_tag = document.createElement("input");
虽然创建了单选按钮,但TextNode
我用
This though created radio button but the TextNode
I appended to the radio button with
document.createTextNode("Radio Label");
不起作用.在这种情况下,它仅显示不带Radio Label
的单选按钮.
虽然我可以看到完整的代码:
does not work. It simply shows the radio button with no Radio Label
as in this case.
Though I can see the complete code:
<input id="my_id" type="radio" name="radio_name">Radio Label</input>
有人可以解释吗?
PS
我遇到的主要问题是自动关闭输入标签,正如我在问题中提到的那样,因为我正在使用var DOM_tag = document.createElement("input");
,它会自动创建一个关闭标签.我该怎么办?
Can anyone explain please ?
P.S.
The main problem that occured to me is the automatically closing of input tag as I mentioned in the question as I am using var DOM_tag = document.createElement("input");
which automatically creates a closing tag. What should I do about that ?
推荐答案
这些是无效元素.这意味着它们并非旨在包含文本或其他元素,因此不需要-实际上,不能-HTML中的结束标记. 1
These are void elements. This means they aren't designed to contain text or other elements, and as such do not need — and in fact, cannot have — a closing tag in HTML.1
但是,他们可以将<label>
与他们相关联:
However, they can have a <label>
associated with them:
<input id="my_id" type="radio" name="radio_name">
<label for="my_id">Radio Label</label>
单选按钮本质上仍然不能包含文本,因此它们接受文本或其他元素作为内容是没有意义的.确实接受文本作为输入的控件的另一个问题:文本内容应该是它的值还是它的标签?为了避免含糊不清,我们有一个<label>
元素可以完全按照其在锡盒上所说的进行操作,并且我们有一个value
属性来表示输入控件的值.
Radio buttons by nature can't contain text anyway, so it wouldn't make sense for them to accept text or other elements as content. Another issue with a control that does accept text as input: should its textual content then be its value, or its label? To avoid ambiguity we have a <label>
element that does exactly what it says on the tin, and we have a value
attribute for denoting an input control's value.
1 XHTML是不同的;根据XML规则,每个标签都必须打开和关闭;这是通过快捷方式语法而不是</input>
标记完成的,尽管后者同样可以接受:
1 XHTML is different; by XML rules, every tag must be opened and closed; this is done with the shortcut syntax instead of a </input>
tag, although the latter is equally acceptable:
<input id="my_id" type="radio" name="radio_name" />
<label for="my_id">Radio Label</label>
这篇关于关闭HTML< input>标签问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!