1. 程式人生 > >Java專案在jsp頁面中引入jquery框架的步驟

Java專案在jsp頁面中引入jquery框架的步驟

http://www.cnblogs.com/wxjnew/p/3831796.html

環境:在Java  web專案中引入juqery框架

工具:MyEclipse8.5

[步驟如下]

A:新建一個Java web專案TestJquery,在WebRoot目錄下建立一個jQuery資料夾

B:下載jquery-1.8.3.min.js放入jquery資料夾中

C:建立jsp頁面

複製程式碼
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
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%>"> <title>My JSP 'test.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <script src="jquery/jquery-1.8.3.min.js">//引入jquery框架 </script> <script type="text/javascript"> function testjquery() { var user_name
=$("#test").attr("value"); alert(user_name); } </script> <body> <input id="test" value="jquery"> <input type="button" value="click me!" onclick="testjquery();"> </body> </html>
複製程式碼

 ps:jquery引入路徑解釋:jquery/jquery-1.8.3.min.js以當前頁面test.jsp為座標在當前資料夾中查詢,由於test.jsp和jquery資料夾都在webroot目錄下所以不需要

 '/',如果加'/'意思是從專案根目錄查詢

D:釋出專案,檢視test.jsp

可以看到點選按鈕 click me !可以取到文字框的值

因為

文字框取值用了jquery的語法$("#test").attr("value"); test是文字框的id

所以

在此jsp頁面中jquery框架引入成功

ps:juqery框架可以根據物件id輕鬆的獲得物件的值.

 本來轉載自:PHP問題錯誤修改--一個IT實時問答系統--快速解決你的任何IT問題,無需等待!---www.itbaiduwang.com