可以使用< span /&gt ;?关闭span [英] Can a span be closed using <span />?

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

问题描述

 < span class =drop/> 

可以这样关闭HTML范围吗?

解决方案

这是否有效取决于您的 doctype



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



更新: 根据以下非常好的评论,如果mime类型 text / xml 应用程序,浏览器将只会正确解释所有自动关闭标签/ xhtml + xml ,请参阅此处了解详情。对于作为 text / html (绝大多数)的页面,请参阅这里可以自动关闭的标签。



此示例将验证:

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

< html xmlns =http://www.w3.org/1999/xhtml>
< head>
< title>< / title>
< / head>
< body>
< h2>将测试页< / h2>
< p>一些内容< span class =drop/>< / p>
< / body>
< / html>但是,此示例无效,因为我已将doctype切换为HTML:

>

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

< html xmlns =http://www.w3.org/1999/xhtml>
< head>
< title>< / title>
< / head>
< body>
< h2>将测试页< / h2>
< p>一些内容< span class =drop/>< / p>
< / body>
< / html>

一些有用的参考资料:




<span class="drop" />

Can HTML spans be closed like this?

解决方案

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

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

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.

This example will validate:

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

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>

A few helpful references:

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

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