JS ajax 例子「建议收藏」

JS ajax 例子「建议收藏」用js,jQuery编写ajax的样式,三种写法,例子://用js原生写法functionsendGet(url){xhr.onreadystatechange=function(){ if(xhr.readyState==4&&xhr.status==200){ //获取服务器响应 document…

大家好,又见面了,我是你们的朋友全栈君。

用 js , jQuery 编写 ajax 的样式,三种写法,例子:

//用 js 原生写法
function sendGet(url){
    xhr.onreadystatechange =function(){
	    if(xhr.readyState == 4 && xhr.status == 200){
	    //获取服务器响应
	    document.querySelector("#show").innerHTML = xhr.responseText;
	    }
	};
    //发送异步请求
	xhr.open("GET",url,true);
	//发送请求
	xhr.send();
}
//jQuery 写法
function sendGet(url){
	$.get(url,function(data){
	document.querySelector("#show").innerHTML = data;
});	
function sendGet(url){
    //用 ajax 写
    $.ajax(
    {
	    url:url,
	    method:'get',
	    success: function(data){
		    document.querySelector("#show").innerHTML = data;
	    },
	    error:function(err){
		    alert(err);
	    }					
    });
}

整体的代码:将项目放在 TomCat 下的 root 文件夹下,浏览器输入 localhost:8080/history.html 即可运行。

history.html 代码:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>首页</title>
		<script src="jquery-1.10.1.js" type="text/javascript" charset="utf-8"></script>
		<style type="text/css">			
			table{
				display: inline-table;
				width: 320px;
				border: 1px solid lightgray;
				border-collapse: collapse;
				vertical-align: top;
				margin-right: 0px;
			}
			td{
				padding: 15px;
				cursor: pointer;
				border: 1px solid lightgray;
				
			}
			tr{
				background: linear-gradient(to right,#f6f6f6,#fff);
			}
			tr:hover{
				background: linear-gradient(to right,#eee,#f6f6f6);
			}
			
			div>div{
				display: inline-block;
				width: 400px;
				height: 300px;
				padding: 10px;
				box-sizing: border-box;
				border: 1px solid lightgray;
				margin-left:0px ;			
			}			
		</style>				
	</head>
	<body>	
		<div style="width: 725px;">
			<table border="1" cellspacing="0" cellpadding="0">
				<tr><td title="java">疯狂java 讲义</td></tr>
				<tr><td title="ee">java ee</td></tr>
				<tr><td title="android"> android</td></tr>
				<tr><td title="html">  html </td></tr>
				<tr><td title="front">front</td></tr>				
			</table>
			<div id="show">				
			</div>
		</div>			
		<script type="text/javascript">		
			var xhr = new XMLHttpRequest();
			window.onload = function(){
				//页面加载时,替换 history 的状态
				history.replaceState(null,"首页","history.html?id=java");
			}
			//获取页面上所有td元素
			var tdList = document.querySelectorAll("td");
			//遍历所有的 td 元素, 为他们的 onclick 时间绑定处理函数
			for(var i =0;i< tdList.length;i++){
				tdList[i].onclick = function(src){
					//以当前单元格的 title为参数发送异步请求
					sendGet("books.jsp?id=" + src.target.title);
					//想 history 压入状态
					history.pushState({"cellTitle":src.target.title},
						"首页","history.html?id="+ src.target.title);
				//取消所有td元素的背景色
				var tdList = document.querySelectorAll("td");
				for(var i=0;i <tdList.length; i++){
					tdList[i].style = undefined;
				}
				//为当前单击的单元格设置背景色
				src.target.style.background = 
						"linear-gradient(to right,#ddd, #eee)";
				}
			}
			/*
			//用 js 原生写法
			function sendGet(url){
				xhr.onreadystatechange =function(){
					if(xhr.readyState == 4 && xhr.status == 200){
						//获取服务器响应
						document.querySelector("#show").innerHTML = xhr.responseText;
					}
				};
				
				//发送异步请求
				xhr.open("GET",url,true);
				//发送请求
				xhr.send();
			}
			*/
			
			//jQuery 写法
			function sendGet(url){
				//$.get(url,function(data){
				//	document.querySelector("#show").innerHTML = data;
				//});	
				
				//用 ajax 写
				$.ajax(
				{
					url:url,
					method:'get',
					success: function(data){
						document.querySelector("#show").innerHTML = data;
					},
					error:function(err){
						alert(err);
					}					
				});
			}
			
			//为窗口的 popstate 事件绑定监听器
			window.addEventListener("popstate",function(){
				//获取history 的状态数据
				var curTitle = history.state["cellTitle"];
				var tdList = document.querySelectorAll("td");
				//取消所有单元格的背景色,只设置被选中的单元格的背景色
				for(var i = 0;i <tdList.length ; i++){
					if(tdList[i].title == curTitle){
					tdList[i].style.background ="linear-gradient(to right,#ddd,#eee)";
					}
					else{
						tdList[i].style = undefined;
					}
				}	
					//通过窗口地址栏获取请求参数
					queryStr = window.location.href.split("?")[1];
					//发送异步请求	
					sendGet("books.jsp?"+ queryStr);
			});			
		</script>					
	</body>
</html>

book.jsp 代码:

<%@page pageEncoding="UTF-8"%>
<%
	String id = request.getParameter("id");
	String result =null;
	if (id.equals("java")) {
		result = "java 书,111111111111111111111111";
	}else if (id.equals("ee")){
		result = "java ee书,2222222222222222222222";
	}else if (id.equals("android")){
		result = "android 书,33333333333333333333333";
	}else if (id.equals("html")){
			result = "html 书,4444444444444444444444444444";
	}else if (id.equals("front")){
			result = "front 书,55555555555555555";    
	}
	out.println(result);	
%>

 

 

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/158768.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 基于Mixin Network的C#语言比特币开发教程 : 用 Mixin Messenger 机器人接受和发送比特币…

    基于Mixin Network的C#语言比特币开发教程 : 用 Mixin Messenger 机器人接受和发送比特币…

    2021年7月2日
    185
  • 继续卷!面试又问Spring 事务有几种传播行为和隔离级别?

    怕什么真理无穷进一步有近一步的欢喜面试又被问到了事务,来吧,要么卷起来,要么躺平。卷不动躺平会不会导致数据不一致?事务概念事务是数据库管理系统执行过程中的一个逻辑单位,由一个有限的数据库操…

    2022年3月1日
    48
  • pycharm怎么配置tensorflow环境_ensp详细安装步骤

    pycharm怎么配置tensorflow环境_ensp详细安装步骤Tensorflow详细安装步骤及PyCharm配置Tensorflow是谷歌开源的深度学习框架,分为两个版本,GPU和CPU,主要的区别在于计算速度,GPU版本要比CPU计算速度更快,适用于处理大量复杂的数据,但需要计算机配置独立NVIDIA显卡。CPU版本没有显卡要求,安装更简单,合适新手小白和学生党,下面介绍CPU版本Tensorflow的详细安装步骤系统环境:Windows10第一步:安装Anaconda两种方式:直接在Anaconda官方网站下载,但速度很慢;建议第二种,选择镜像网站下载,

    2022年8月26日
    6
  • Apache配置虚拟主机,关于403问题的解决

    Apache配置虚拟主机,关于403问题的解决安装wamp集成开发环境后,配置虚拟主机,在浏览器中输入虚拟主机的域名,出现403forbidden的错误.在以前的开发工作中就遇到过这种情况,之前一直困扰着我,通过网上搜索,终于解决了这个问题。出现这个问题的原因是目录访问权限没有设置。       具体解决步骤如下:1 打开apache配置文件httpd.conf,找到目录权限的语句   O

    2022年9月17日
    0
  • dirsearch安装和使用[通俗易懂]

    dirsearch安装和使用[通俗易懂]目录dirsearch介绍下载及安装如何使用简单用法递归扫描线程前缀/后缀黑名单筛选器原始请求Wordlist格式排除扩展扫描子目录代理报告其他命令小贴士选项选项强制性字典设置一般设置请求设置连接设置配置dirsearch介绍dirsearch是一个基于python3的命令行工具,常用于暴力扫描页面结构,包括网页中的目录和文件。相比其他扫描工具disearch的特点是:支持HTTP……

    2022年10月6日
    0
  • 草单的_单字草书写法

    草单的_单字草书写法http://blog.sina.com.cn/s/blog_4d6c45250100x0t1.html

    2022年10月19日
    0

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号