且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

关闭HTML输入标签问题

更新时间:2023-09-18 12:08:10

这些是无效的元素。这意味着它们没有被设计为包含文本或其他元素,因此不需要 - 实际上,不能有 - HTML中的结束标记。 1



然而,他们可以有一个< label> 与它们相关联:

 < input id =my_idtype =radioname =radio_name> 
< label for =my_id>无线标签< / label>

大自然的单选按钮无论如何都不能包含文字,所以它们对他们来说没有任何意义接受文字或其他元素作为内容。接受文本作为输入的控件的另一个问题是:其文本内容是否应该是其值或标签?为了避免模棱两可,我们有一个< label> 元素,它完全符合它所说的,并且我们有一个属性来表示输入控件的值。




1 XHTML不同;通过XML规则,每个标签都必须打开和关闭;这是通过快捷语法来完成的,而不是< / input> 标签,尽管后者同样可以接受:

 < input id =my_idtype =radioname =radio_name/> 
< label for =my_id>无线标签< / label>


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 ?

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>.

I personally felt the problem when I created a input tag for Radio buttons using

var DOM_tag = document.createElement("input");

This though created radio button but the TextNode I appended to the radio button with

document.createTextNode("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>

Can anyone explain please ?

PS : 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 ?

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

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>

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.


1XHTML 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>