标签与跨度:html

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12890882/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 03:23:05  来源:igfitidea点击:

Label vs span : html

html

提问by jason

I am a bit confused in what to use for rendering data. My scenerio is that I have to render count and I am not sure about using span or label.

我对用于渲染数据的内容有些困惑。我的场景是我必须渲染计数,我不确定使用跨度或标签。

<span id="spnCount"></span>

or

或者

<label id="lblCount"></label>

回答by slapthelownote

A labelis used when you have a form or input elements - the labelis associated with an inputelement. Span is a general container for any inline content. I think you want a spanin this case

label当您有表单或输入元素时使用A -labelinput元素相关联。Span 是任何内联内容的通用容器。我想span在这种情况下你想要一个

回答by Ron van der Heijden

Span

跨度

The <span>tag is used to group inline-elements in a document.

<span>标签用于对文档中的行内元素进行分组。

The <span>tag provides no visual change by itself.

<span>标签本身不提供视觉变化。

The <span>tag provides a way to add a hook to a part of a text or a part of a document.

<span>标签提供了一种钩添加到文字或文档的一部分的一部分。

Label

标签

The <label>tag defines a label for an element.

<label>标记定义为一个元素的标签。

The <label>element does not render as anything special for the user. However, it provides a usability improvement for mouse users, because if the user clicks on the text within the <label>element, it toggles the control.

<label>元素不会呈现为用户的任何特殊内容。但是,它为鼠标用户提供了可用性改进,因为如果用户单击<label>元素内的文本,它会切换控件。

The for attribute of the <label>tag should be equal to the id attribute of the related element to bind them together.

<label>标签的 for 属性应该等于相关元素的 id 属性将它们绑定在一起。

回答by Ron van der Heijden

Every single person who "answered", simply copy-pasted documentation that describes the intended use-case. However, nobody explained the differences, or the WHYbehind why you SHOULDuse one or the other.

每个“回答”的人,只需复制粘贴描述预期用例的文档。但是,没有人解释的差异,或为什么落后,为什么你应该使用一个或另一个。

The reality is, either one will technically work, so it honestly does not even matter. You could use any number of other similar tags, including the <i>tag (properly styled with a class). Assign it a unique id, use document.getElementById(), or use nodes, etc, do what you want. The only people who care are semantic purists.

现实情况是,从技术上讲,任何一个都可以工作,因此老实说,这甚至无关紧要。您可以使用任意数量的其他类似标签,包括<i>标签(使用类正确设置样式)。为它分配一个唯一的 id,使用document.getElementById()或使用节点等,做你想做的。唯一关心的人是语义纯粹主义者。

The label "can" and "should" be used with a form input element, true. But it is NOT TRUEthat it CAN NOTor MUST NOTbe used in any other way. Which means it CANand COULDbe used in other ways.

标签“可以”和“应该”与表单输入元素 true 一起使用。但它是不是真的,它CAN NOT不得以任何其它方式使用。这意味着它CANCOULD以其他方式使用。

First of all, notice how <label>is simply used as a label "for an element". It does not say what TYPEof element. It does not say it must be an input element or a form element. Indeed, non-form elements can have controls associated with them for provide a user experience, which perhaps manipulate objects with CSS and so on, and a label may simply connect the text to any such things. The form could exist invisibly on page. Who knows.

首先,注意 how<label>简单地用作“元素”的标签。它没有说明元素的类型。它没有说它必须是输入元素或表单元素。实际上,非表单元素可以具有与其关联的控件以提供用户体验,这些控件可能使用 CSS 等操作对象,并且标签可以简单地将文本连接到任何此类事物。表单可以不可见地存在于页面上。谁知道。

Why might it be a bad idea to use a standalone label? If your document might exist for a long time, through several browser versions and HTML standards updates, the definition of <label>might change to more strictly enforce association with form input elements, then you might have a problem. But the same is true for just about any other aspect of any of a number of specifications we rely upon just to render a page.

为什么使用独立标签可能是个坏主意?如果您的文档可能存在很长时间,通过多个浏览器版本和 HTML 标准更新,定义<label>可能会更改为更严格地强制执行与表单输入元素的关联,那么您可能会遇到问题。但是对于我们仅依赖于渲染页面的许多规范中的任何其他方面,情况也是如此。

It's probably a very rare scenario that such a change would occur, and you'd likely not even work at the same company or on the same project team, so in all honesty, who really cares except purists?

发生这样的变化可能是一种非常罕见的情况,你甚至可能不在同一家公司或同一个项目团队工作,所以老实说,除了纯粹主义者,谁真正关心?

Well, maybe anyone who is visually impaired if they rely on some technology that treats a <label>different than <span>, which could confuse the technology or the user or both. I don't have any experience with such accessibility devices, but that might be a better reason WHY.

好吧,也许任何有视力障碍的人,如果他们依赖某种<label>不同于<span>的技术,这可能会混淆技术或用户或两者。我对此类无障碍设备没有任何经验,但这可能是一个更好的理由WHY.

Another valid reason is <span>is shorter to type than <label>.

另一个有效的原因是<span>输入比<label>.

And another reason might be subtle differences in the way that a search engine ranks your page or references content if using <label>vs <span>. This is a bit of a stretch, because such algorithms are generally not publicly available, but it's possible. One engine might produce better results one way, another engine may prefer the other way, and another engine may not care either way.

另一个原因可能是搜索引擎在使用<label>vs 时对您的页面进行排名或引用内容的方式存在细微差异<span>。这有点牵强,因为此类算法通常不公开可用,但它是可能的。一个引擎可能会以一种方式产生更好的结果,另一个引擎可能更喜欢另一种方式,而另一个引擎可能不关心任何一种方式。

All that said, without any further knowledge of context, I'd probably go with <span>as it seems the most genericand least contentiousway of doing things. But I felt the question lacked a thorough answer, as answers usually involve a comprehension of why.

综上所述,在没有进一步了解上下文的情况下,我可能会采用<span>它,因为它似乎是最通用争议最少的做事方式。但我觉得这个问题缺乏彻底的答案,因为答案通常涉及对原因的理解。

回答by Marat Tanalin

If your data is a result of calculation, the outputHTML5-element probably fits best for your purpose.

如果您的数据是计算结果,则outputHTML5 元素可能最适合您的目的。

回答by hkutluay

The tag defines a label for an element. If you are not using for input element, span can be used.

标签定义了一个元素的标签。如果您不使用 for input 元素,则可以使用 span。

回答by Samich

labelis used for labeling form controls in html. It also has forattribute where you can set id of the control which this label related to. spanused in case when you need to display some literal data.

label用于在 html 中标记表单控件。它还具有for属性,您可以在其中设置与此标签相关的控件的 id。span当您需要显示一些文字数据时使用。

回答by jao

A label is used in combination with a form input element. In some browsers the label can be clicked to activate the input element.

标签与表单输入元素结合使用。在某些浏览器中,可以单击标签以激活输入元素。

回答by CaptainCarl

"The <label>tag defines a label for an <input>element." http://www.w3schools.com/tags/tag_label.asp

<label>标签定义了一个<input>元素的标签。” http://www.w3schools.com/tags/tag_label.asp