Jquery摘要

来源:互联网 发布:网络电视上的直播软件 编辑:程序博客网 时间:2024/06/02 11:33

1 绑定事件传递参数

$("#Android_Jump_Content").bind('click', 'Android', display_jump_content)

不能直接在函数后面添加参数,否则表示函数调用,函数调用的结果绑定在click事件上

$("#Android_Jump_Content").bind('click',  display_jump_content('Android'))


2 判断元素是否存在

 var table = $("#to_add_jump_table")
 if (table == undefined || null == table || table.length == 0){
     //元素不存在
 }


3 在事件方法中,根据事件对象获取

     $("#to_add_jump_table .glyphicon-remove").on('click', function () {
        console.log('remove record')
        var record = $(this).parent().parent()
        record.remove()
        //$("#to_add_jump_table").removeChild(record)
    })

需要用$(this),把事件对象转化为jquery对象


4 ajax error方法,当响应码为204,则也会进入到error方法。

  (如果服务端响应码为200,但是没有响应体,则ajax做视为204处理)

    $.ajax({
        type: "GET",
        url: "/jump/xx",
        data: {"type": osType},
        dataType: "json",
        async: true,
        success: function (data) {
            console.log('success to deploy')
            display_jump_content(event)
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            console.log('fails to deploy')
            console.log(XMLHttpRequest.readyState + XMLHttpRequest.status + XMLHttpRequest.responseText)
            handle_jump(null)
        }
    });

 说明,和参数 dataType: "json" 有关,如果没有设置 dataType: "json",则没有响应体时,则进入到success方法

0 0
原创粉丝点击