Html 什么是 <span></span> 元素?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1096251/
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
What is <span></span> element?
提问by MAC
What is the purpose of the
目的是什么
<span></span>
element? And where should we use it?
元素?我们应该在哪里使用它?
回答by Xn0vv3r
From here:
从这里:
Definition and Usage
The tag provides no visual change by itself.
The tag provides a way to add a hook to a part of a text or a part of a document.
When the text is hooked in a span element you can add styles to the content, or manipulate the content with for example JavaScript.
定义和用法
标签本身不提供视觉变化。
标签提供了一种向文本的一部分或文档的一部分添加钩子的方法。
当文本在 span 元素中挂钩时,您可以向内容添加样式,或使用 JavaScript 等操作内容。
So, the short version is, it's to style some text (without a paragraph or linebreak which is different from <div> tag)
所以,简短的版本是,它是一些文本的样式(没有与 <div> 标签不同的段落或换行符)
回答by mas
It is for grouping HTML elements and content for applying structure, defined for HTML 4.0 at http://www.w3.org/TR/REC-html40/struct/global.html#h-7.5.4:
它用于对 HTML 元素和内容进行分组以应用结构,为 HTML 4.0 定义在 http://www.w3.org/TR/REC-html40/struct/global.html#h-7.5.4:
The DIV and SPAN elements, in conjunction with the id and class attributes, offer a generic mechanism for adding structure to documents. These elements define content to be inline (SPAN) or block-level (DIV) but impose no other presentational idioms on the content. Thus, authors may use these elements in conjunction with style sheets, the lang attribute, etc., to tailor HTML to their own needs and tastes.
DIV 和 SPAN 元素与 id 和 class 属性一起提供了一种向文档添加结构的通用机制。这些元素将内容定义为内联 (SPAN) 或块级 (DIV),但不会对内容施加其他表现形式。因此,作者可以将这些元素与样式表、lang 属性等结合使用,以根据自己的需要和品味定制 HTML。
It goes on to give an example of the usage.
它继续给出一个用法的例子。
回答by joeslice
It's just a way to logically group subelements together. You can also apply styles, etc at this level which will cascade down.
这只是将子元素逻辑分组在一起的一种方式。您还可以在此级别应用样式等,这将向下级联。
Take a look at the wikipedia articlewhich goes into some depth.
看一看维基百科文章,它有一定的深度。
回答by Nenotlep
In addition to styling/grouping/scripting, spans can give actual functionality and/or meaning to bits of text. Consider these:
除了样式/分组/脚本之外,跨度还可以为文本位提供实际的功能和/或意义。考虑这些:
<p>This <span title="Punk IPA">beer</span> tastes good.</p>
<p>Come on, <span lang="kr">??</span>!`</p>
<p>Hello <span contenteditable="true">Wordl</span>!?</p>
Also the dir, translate or spellcheck attributes are not only for CSS or for JS, but usable on their own too!
此外,dir、translate 或拼写检查属性不仅适用于 CSS 或 JS,还可以单独使用!
回答by driis
Span has no structural meaning to the document (as h1 does, for instance), but can be used as a container when none of the other inline containers are appropiate. You would, for instance, use a span if you needed to apply some CSS to a small bit of text within some other text.
Span 对文档没有结构意义(例如 h1),但是当其他内联容器都不合适时,它可以用作容器。例如,如果您需要将一些 CSS 应用于其他文本中的一小段文本,则可以使用跨度。
回答by Arnkrishn
The span tag just tells the browser to apply what ever style changes are included within the span and if there is no styling within the span then there would be no formatting applied to the enclosed text. This is a bit different from the div tag which incorporates a paragraph break even if no styling options are included within a div.
span 标签只是告诉浏览器应用 span 中包含的任何样式更改,如果 span 中没有样式,则不会将格式应用于封闭的文本。这与 div 标签有点不同,即使 div 中没有包含样式选项,它也会包含一个段落分隔符。
So it is useful when you want to apply styles to elements without placing them in a separate block.
因此,当您想将样式应用于元素而不将它们放在单独的块中时,它很有用。
cheers
干杯
回答by janetsmith
It's just a placeholder for javascript or css.
它只是 javascript 或 css 的占位符。
If you don't use javascript/css, has no use to u.
如果你不使用 javascript/css,对你没有用。