jquery笔记(view:2832)

1.$.unique()
用于对DOM元素数组进行排序,并移除重复的元素。

2.$.when()
使用$.when()的方式会等到两个请求都返回之后才触发回调函数

$.when(
    $.getJSON('a.json'),
    $.getJSON('b.json')
).done(function(a,b){});

链接:http://www.ruanyifeng.com/blog/2011/08/a_detailed_explanation_of_jquery_deferred_object.html

3.replaceWith

$('p').each(function(){
    $(this).replaceWith('<b>'+$(this).html()+'</b>');
});
//替换标签,保持原内容
<div>
    <i>苹果</i>
    <i>电脑</i>
</div>
<script>
var translate = {
    '苹果' : 'apple',
    '电脑' : 'PC'
};
$('i').each(function(){
    $(this).replaceWith(translate[$(this).html()]);
});
</script>
//翻译替换

4.end()

5.ajax参数type为post,URL部分路径问题

url: '/api/ip/'                //这种路径方式type为post可以成功传值
url: '/api/ip/index.php'       //这种路径方式type为post可以成功传值
url: '/api/ip'                 //这种路径方式type为post时候传值失败
                               //以上路径方式,get方式都可以成功传值

6.函数通过return返回ajax数据
方法如下:
1. ajax 必须为同步 设置async:false
2. 定一个局部变量 把data赋值给局部变量 然后 return 局部变量即可

function getProvData() {  
    var provData = "";  
    $.ajax({  
        url: "/api/prov/",  
        async:false,  
        data: {},  
        success: function (json) {  
            provData = json;  
        }  
    });  
    return provData;  
}