jquery和ajax

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);
            }
        })
    })

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值