jquery是js库,在百度搜索jquery之后即可下载
目前版本为3.3.1,两个版本压缩版本和未压缩版本,一般生产环境使用压缩版本
使用方法:
<script type="text/javascript" src="jquery-3.3.1.js"></script>引用下载下俩的js即可
jquery使用语法:
$("*").removeClass("eg")为所有的类删除eg类属性
$("*").addClass("eg")为所有的类添加eg类属性
$(“#id”) 针对特定id的选择器
$("标签")针对特定标签的选择器
$(".class")针对特定的类的选择器
ajax是一种可以不用转移到其他页面,在本页面通过后台动态展示页面的技术。前端通过js将请求发送给后端,后端直接给前端的当前页面响应,前端在通过处理后端的响应动态显示页面
前端js代码示例
$("#btnload").on("click", function(){
var xmlhttp;
if(window.XMLHttpRequest)
{
xmlhttp = new XMLHttpRequest;
}
else
{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
console.log(xmlhttp)
xmlhttp.open("GET", "/Session/AnserAj", true);
xmlhttp.send();
console.log(xmlhttp.responseText);
xmlhttp.onreadystatechange = function(){
if(xmlhttp.readyState == 4 && xmlhttp.status == 200)
{
var t = xmlhttp.responseText;
console.log(t);
document.getElementById("divContene").innerHTML = t;
}
}
console.log(xmlhttp)
})
使用fastjson可以很方便的在后台传输json给前台
String json = JSON.toJSONString(ans);
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
response.getWriter().println(json);
前台可以使用JSON.parse方法,将解析出的json循环输出即可
jquery对ajax的简化
$(function(){
$.ajax({
"url" : "/Session/AnsJqAjax",//请求地址
"type" : "get",//method
"data" : {"t" : "zjx"},//request的参数
"dataType" : "json",//回传的数据格式
"success" : function(json){//成功的处理函数,一般用于修改前台
console.log(json);
}
})
})