js开发心得之一


1.启动后事件绑定:
$(function () {....})//Shorthand for $( document ).ready()
注意:
Code included inside $( document ).ready() will only run once the page Document Object Model (DOM) is ready for JavaScript code to execute.
Code included inside $( window ).load(function() { ... }) will run once the entire page (images or iframes), not just the DOM, is ready.
2.事件绑定:
$(".类名").each(function () {
        $(this).bind('click', function () {
            元素要处理的逻辑。。。。
        });
    });

最好有两个函数:
xx_unbind(), xx_bind()。把所有的事件绑定都放在里面,对于ajax运用过程中,绑定事件会有些问题。一般先把所有的绑定解除,然后再绑定。

3. AJAX
get请求:
$.ajax({
        url: "网址",
        type: "GET",
        data: {
            key1: val1,
            key2: val2,
            key3: val3
        },

        success: function (data, status, xhr) {
            数据成功后的逻辑
        }
    });

post请求:
$.ajax({
           url: "ppp/",
           type: "POST",
           data: {
                is_add_comment: true,
                answer_id: answer_id,
                new_comment: new_comment,
                commenter_id: commenter_id
             },
  //有些服务器要加如下的东西。django就要。
           beforeSend: function (xhr, settings) {
                if (!this.crossDomain)
                    xhr.setRequestHeader("X-CSRFToken", csrftoken);
           },

           success: function (data, status, xhr) {
         成功后的逻辑。。
            }
        });
4.元素文字操作:
input系列的editablearea.val(cur_answer);
span系列, span.html("Submit Answer");

5.元素的增加:
 var button_span = $("<span></span>");
 button_span.attr("class", "font-question-thanks-button");
 button_span.html("Submit Answer");

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值