jQuery学习笔记——ajax

<div class="letter" id="letter-a">
	<h3>
		<a href="#">A</a>
	</h3>
</div>
<div id="dictionary">
</div>

 

$(document).ready(function () {
	$("#letter-a a").click(function () {
		$.get("jqueryGet", {"term":$(this).attr('href')}, function (data) {
			$("#dictionary").html(data);
		});
		alert("test");
		return false;
	});
});

 

public class JqueryGet extends HttpServlet {

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		System.out.println("POST");
		System.out.println(request.getParameter("term"));
		
		// 返回值为字符串类型,相应的head设置为text/html
		response.setContentType("text/html;charset=UTF-8");

		PrintWriter out = response.getWriter();

		out.write("<h3 class=\"term\">ABDICATION</h3>");
	}

	// Process the HTTP Get request
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		System.out.println("GET");
		doPost(request, response);
	}

}

 

说明:

1$.get方法的第一个参数是链接后台的URL

2$.get方法的第二个参数是传递给后台的Parameter

3$(this)是点击的对象,$(this).attr(‘href’)的值是’#’$(this).text()的值是’A’

4$.get方法的第三个参数是回调函数,参数data可以是字符串,也可以是dom对象或者json对象

5$(“#dictionary”).html(data)相当于setHTML

6$(“#dictionary”).html()相当于getHTML

7alert(“test”)可能会先于回调函数执行,这就是异步调用

8return false是为了防止单击这些链接时打开新的URL,因此在事件处理程序中必须返回false

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值