今天在jsp文件中导入jquery,发现无论是远程导入还是本地导入,均没反应,源码如下:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<!-- <script type="text/javascript" src="http://code.jquery.com/jquery-2.2.0.min.js"/> -->
<script type="text/javascript" src="jquery/jquery-2.2.0.min.js"/>
<script type="text/javascript">
$(document).ready(function() {
alert("hello world");
});
$(function() {
// 網頁完整讀取完畢後執行,jQuery允許重複定義$(function(){});
alert("hello world,again!");
});
</script>
</head>
<body>
test
</body>
</html>
查了大半天,发现<script>标签写错,不可写成<script/>,因为script标签不是自闭合的,这样写无效。自闭合标签有:
area、base, br, col, command, embed, hr, img, input, keygen, link, meta, param, source, track, wbr
改成这样即可:
script type="text/javascript" src="http://code.jquery.com/jquery-2.2.0.min.js"></script>
如果导入本地jquery.js,如我将我的jquery-2.2.0.min.js放在了WebContent/jquery目录下,这时本地导入地址应该是:
<script type="text/javascript" src="jquery/jquery-2.2.0.min.js"></script>
前提是定义了basePath(http://localhost:8080/项目名称/),即相对于basePath,如:
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
………………
参考:
http://www.cnblogs.com/dangjian/p/4238150.html