I'd like to include a javascript file on every page of a site. I can do this with:
我希望在站点的每个页面都包含一个javascript文件。我可以这样做:
<script type="text/javascript" src="myFile.js" ></script>
This works fine - however as there's nothing between the two tags, I wanted to change it to:
这很好——但是由于两个标签之间没有任何区别,我想把它改为:
<script type="text/javascript" src="myFile.js" />
If I do this, it doesn't work - nothing after that line loads on the page.
如果我这样做,它就不起作用了——在页面上加载了一行之后什么都没有。
Any ideas why? Thanks
任何想法为什么?谢谢
3 个解决方案
#1
32
Unfortunately, the HTML specs for REQUIRE a closing tag...
不幸的是,HTML规范需要一个结束标记……
HTML Standard, section 18.2.1
18.2.1 HTML标准,部分
18.2.1 The SCRIPT element
18.2.1脚本元素
Start tag: required, End tag: required
起始标签:required,结束标签:required
#2
3
This is not a bug, it's standard behavior.
这不是bug,而是标准行为。
Also, empty HTML elements are often not rendered:
此外,空的HTML元素通常不会呈现:
<div style="background:red"></div>
displays, <div style="background:red" />
doesn't
display, 没有
#3
2
HTML
doesn't support self closing tags. If you want to use them you need to use an xml based doctype AND serve the file as xml.
HTML不支持自闭标签。如果您想使用它们,您需要使用基于xml的doctype并将文件作为xml提供。
XHTML or the xml serialisation of html5 would both work.
XHTML或html5的xml序列化都可以工作。
Here is an example:
这是一个例子:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>XHTML5 Template</title>
<meta charset="utf-8" />
<script type="text/javascript" src="http://documentcloud.github.com/underscore/underscore-min.js" />
</head>
<body>
</body>
</html>
Save this in a file with a .xhtml
extension and open it in a modern browser and the self closing tag will work.
将其保存在一个带有.xhtml扩展的文件中,并在一个现代浏览器中打开它,并且自闭标签将起作用。
#1
32
Unfortunately, the HTML specs for REQUIRE a closing tag...
不幸的是,HTML规范需要一个结束标记……
HTML Standard, section 18.2.1
18.2.1 HTML标准,部分
18.2.1 The SCRIPT element
18.2.1脚本元素
Start tag: required, End tag: required
起始标签:required,结束标签:required
#2
3
This is not a bug, it's standard behavior.
这不是bug,而是标准行为。
Also, empty HTML elements are often not rendered:
此外,空的HTML元素通常不会呈现:
<div style="background:red"></div>
displays, <div style="background:red" />
doesn't
display, 没有
#3
2
HTML
doesn't support self closing tags. If you want to use them you need to use an xml based doctype AND serve the file as xml.
HTML不支持自闭标签。如果您想使用它们,您需要使用基于xml的doctype并将文件作为xml提供。
XHTML or the xml serialisation of html5 would both work.
XHTML或html5的xml序列化都可以工作。
Here is an example:
这是一个例子:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>XHTML5 Template</title>
<meta charset="utf-8" />
<script type="text/javascript" src="http://documentcloud.github.com/underscore/underscore-min.js" />
</head>
<body>
</body>
</html>
Save this in a file with a .xhtml
extension and open it in a modern browser and the self closing tag will work.
将其保存在一个带有.xhtml扩展的文件中,并在一个现代浏览器中打开它,并且自闭标签将起作用。