关闭HTML输入标签问题

关闭HTML输入标签问题

本文介绍了关闭HTML输入标签问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道为什么HTML < input> 标签没有像其他HTML标签那样得到结束标签,并且如果我们关闭输入标签会出现什么问题?



我尝试Google,并且发现了标准输入标签,如< input type =textname =name > 不会以< / input> 来关闭它。 我个人当我使用

 创建 Radio 按钮的输入标签时, var DOM_tag = document.createElement(input); 

虽然已创建单选按钮,但 TextNode 我追加到单选按钮上,并带有

  document.createTextNode(Radio Label); 

不起作用。它只显示单选按钮,没有 Radio Label ,就像这种情况一样。
尽管我可以看到完整的代码,但是我们仍然可以看到完整的代码:

 < input id =my_idtype =radioname = radio_name>无线电标签< /输入> 

任何人都可以解释一下吗?


PS:主要问题是发生在我身上的是输入标签的自动关闭,正如我在问题中提到的那样,因为我正在使用 var DOM_tag = document.createElement(input); ,它会自动创建一个关闭标签。我应该怎么做?

解决方案

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



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

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

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






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

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.


<input id="my_id" type="radio" name="radio_name" />
<label for="my_id">Radio Label</label>

这篇关于关闭HTML输入标签问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 13:47