---
title: "js开发心得之一"
author: "美丽的小岛"
date: "2015-08-16T10:01:21Z"
tags: []
categories: ["编程语言"]
slug: "js"
draft: false
lastmod: "2015-08-16T10:01:21Z"
id: 2235751
url: "https://happyprince.iteye.com/blog/2235751"
---

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 = $("&lt;span&gt;&lt;/span&gt;");  
 button\_span.attr("class", "font-question-thanks-button");  
 button\_span.html("Submit Answer");