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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • ps快捷键常用表实用表_计算机查找快捷键

    ps快捷键常用表实用表_计算机查找快捷键PS是一款使用最多的图片处理软件,不论是普通玩家还是专业的制图用户都在用。今天来给大家分享ps快捷键常用表,方便大家参考学习使用,在制图的时候更加的便捷。【1】CTRL+SHIFT+单击(选择多个对象)【选择工具】非”自动选择“状态下:1.按CTRL+左键可以选择对象2.按CTRL+SHIFT+左键可以选择多个对象【2】空格+点击(按住状态)(可移动选区)绘制一个选框、矢…

    2022年9月29日
    2
  • linux修改密码长度限制_linux文件名长度限制修改

    linux修改密码长度限制_linux文件名长度限制修改修改密码长度:设置为不少于8位的。修改最短密码长度需要编辑login.defs文件(vi/etc/login.defs),把下面这行PASS_MIN_LEN5(默认的情况)改为PASS_MIN_LEN8(修改后的情况)login.defs文件是login程序的配置文件 转载于:https://blog.51ct…

    2025年9月19日
    4
  • Pyinstaller打包exe完整教程

    Pyinstaller打包exe完整教程原创文|Space9Python文件打包成可安装、无需Python依赖的高效可执行exe程序工具及环境PyInstallerInnoSetupWindows和PythonPyInstaller打包Python应用程序为独立的可执行文件安装PyInstallerpypi镜像使用帮助https://mirrors.tuna.tsinghua.edu.cn/help/pypi/pipinstall-ihttps://pypi.tuna.tsinghua.edu.cn/simple

    2022年6月25日
    33
  • js算法初窥05(算法模式02-动态规划与贪心算法)

    在前面的文章中(js算法初窥02(排序算法02-归并、快速以及堆排)我们学习了如何用分治法来实现归并排序,那么动态规划跟分治法有点类似,但是分治法是把问题分解成互相独立的子问题,最后组合它们的结果,而

    2022年3月25日
    35
  • STS用Maver创建SpringBoot工程

    STS用Maver创建SpringBoot工程

    2021年7月21日
    59
  • GoogleGoogle搜索解析

    GoogleGoogle搜索解析GoogleGoogle搜索解析是一个类似Google趋势SEO在线keyword工具。它的正式口号提出“在搜索些什么”。利用Google搜索解析,能够比較特定区域、类别、时间范围以及搜索资源之间的搜索量模式,这点也是Google搜索解析相比仅仅能提供基本信息的Googlekeyword工具。Google趋势。Google站点管理员工具以及GoogleAnalytic…

    2022年6月16日
    48

发表回复

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

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