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

猜你喜欢

转载自happyprince.iteye.com/blog/2235751
今日推荐