可以使用 <span/> 关闭跨度吗? [英] Can a span be closed using <span />?

查看:26
本文介绍了可以使用 <span/> 关闭跨度吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<span class="drop" />

HTML spans 可以这样关闭吗?

Can HTML spans be closed like this?

推荐答案

这是否有效取决于您的doctype,基本上无论您使用的是 XHTML 还是 HTML.

Whether or not this is valid depends on your doctype, basically whether or not you're using XHTML or HTML.

当使用 XHTML 时,所有主流浏览器都将支持像您提供的示例那样的自关闭标签.以下面的例子为例,这是有效的,因为我指定页面使用 XHTML(换句话说,HTML 是有效的 XML).

When using XHTML, all major browsers will support self closing tags like the example you provided. Take the following example, this is valid because I'm specifying the page is using XHTML (in other words, HTML that is valid XML).

更新:基于以下非常好的评论,如果 mime 类型为 text/xmlapplication/,浏览器只会正确解释所有自关闭标签xhtml+xml,见这里细节.对于用作 text/html(绝大多数)的页面,请参见 此处 此处用于可自动关闭的标签.

Update: Based on the very good comments below, browsers will only interpret all self closing tags correctly if the mime type is text/xml or application/xhtml+xml, see here for the details. For pages served as text/html (the vast majority), see here here for the tags that can be self closing.

此示例将验证:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>
<body>
    <h2>Will test page</h2>
    <p>some stuff <span class="drop" /></p>
</body>
</html>

然而,这个例子是无效的,因为我已经将文档类型切换为 HTML:

However, this example is not valid, because I've switched the doctype to HTML:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>
<body>
    <h2>Will test page</h2>
    <p>some stuff <span class="drop" /></p>
</body>
</html>

一些有用的参考资料:

这篇关于可以使用 &lt;span/&gt; 关闭跨度吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆