<script> tag doesn't close properly

3
在包含页面基本布局的default.jspx文件中,我尝试按如下方式导入一些jQuery库。
<head>
    ...
    <spring:url value="/resources/js/lib/jquery-1.9.1.min.js" var="jquery_url" />
    <spring:url value="/resources/js/lib/jquery.tokeninput.js" var="jquery_tokeninput_url" />
    <script src="${jquery_url}" type="text/javascript"></script>    
    <script src="${jquery_tokeninput_url}" type="text/javascript"></script>
    <script type="text/javascript">
        $.noConflict();
    </script>
    <util:load-scripts />
    ...
</head>

但是当页面在浏览器中呈现时,第一个script标签会吞噬掉其他两个标签。
<head>
...
<script type="text/javascript" src="/roo-inari/resources/js/lib/jquery-1.9.1.min.js">
//These lines are inside the first script tag
<script type="text/javascript" src="/roo-inari/resources/js/lib/jquery.tokeninput.js"/>
<script type="text/javascript">
        $.noConflict();
//The tag is closed here
</script>
<link href="/roo-inari/resources/dijit/themes/tundra/tundra.css" type="text/css" rel="stylesheet">
...

你知道这是什么原因吗?该项目基于Spring Roo生成的Web MVC脚手架。我正在使用Chrome v.25浏览器。


我不熟悉Spring,但如果你的JSP源代码在解析服务器变量的两行末尾有</script>,那么似乎是Spring或其他服务器输出了它。如果你没有从实际页面源代码中复制它(右键单击,查看源代码),请展示一下。 - Jared Farrish
1
+1 我不确定为什么会有人踩。虽然我不了解 [tag:spring-mvc],但是这似乎很奇怪,因为您的脚本标签看起来格式良好,在页面中也很好看,假设 src 是 [tag:spring-mvc] 的有效值。 - Nope
+1 不确定为什么会被踩(如果您知道,请解释一下),我也遇到了完全相同的问题,这篇文章描述得非常完美。 - Chris Thompson
1个回答

6
简单的解决方法是在标签内写一个注释,这样它就不会自动关闭了。我真是太傻了。
<script src="${jquery_url}" type="text/javascript"><!-- required for some browsers --></script> 

2
+1,因为这听起来对未来遇到类似问题的用户可能很有用。一旦 SO 允许,也请将此答案标记为已接受。 - Nope
这个问题的解释是,当一个元素内部没有任何内容时,解析器会吞掉结束标签。正如@Pyhtos建议的那样,在空脚本标签中放置注释可以解决这个问题。我一年前遇到过这个问题,我相信这个解释与XHTML有关。 - CodeChimp

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接