jsp文件導入jquery.js及自閉合標籤

今天在jsp文件中導入jquery,發現不管是遠程導入仍是本地導入,均沒反應,源碼以下:javascript

<%@ 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標籤不是自閉合的,這樣寫無效。自閉合標籤有:html

area、base, br, col, command, embed, hr, img, input, keygen, link, meta, param, source, track, wbrjava

改爲這樣便可:jquery

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目錄下,這時本地導入地址應該是:jsp

<script type="text/javascript" src="jquery/jquery-2.2.0.min.js"></script>


前提是定義了basePath(http://localhost:8080/項目名稱/),即相對於basePath,如:ui

<%
	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%>">
………………

參考:spa

http://www.cnblogs.com/dangjian/p/4238150.htmlcode

相關文章
相關標籤/搜索