Jquery 方法學習

 

1.Ajax的調用已知
$.get /$.post 
同步執行Ajax  $.ajaxSetup({ async : false});
 
2.$.extend方法擴展
2.1 合併多個對象。
var Css1={size: "10px",style: "oblique"} 
var Css2={size: "12px",style: "oblique",weight: "bolder"} 
$.jQuery.extend(Css1,Css2) 
//結果:Css1的size屬性被覆蓋,並且繼承了Css2的weight屬性 
// Css1 = {size: "12px",style: "oblique",weight: "bolder"} 
2.2 深度嵌套對象。 
jQuery.extend( true, 
{ name: 「John」, location: { city: 「Boston」 } }, 
{ last: 「Resig」, location: { state: 「MA」 } } 
); 
// 結果 
// => { name: 「John」, last: 「Resig」, 
// location: { city: 「Boston」, state: 「MA」 } } 
</span> 

2.3 能夠給jQuery添加靜態方法。 javascript

$.extend({ 
add:function(a,b){return a+b;}, 
minus:function(a,b){return a-b}, 
multiply:function(a,b){return a*b;}, 
divide:function(a,b){return Math.floor(a/b);} 
}); 
var sum = $.add(3,5)+$.minus(3,5)+$.multiply(3,5)+$.divide(5,7); 
console.log(sum); 
 
3 push 方法
var arr = new Array(3)
arr[0] = "George"
arr[1] = "John"
arr[2] = "Thomas"

document.write(arr + "<br />")
document.write( + "<br />")
document.write(arr)
arr.push("James")

輸出:java

George,John,Thomas
4
George,John,Thomas,James
4 call 和apply
        var testO = { name: "Lily" };
        function funcA(a,b) {
            alert(this);
            alert("Function A");
        }

 

        function funcB(a, b) {
            funcA.call(testO, a, b);
        }
ajax

        funcB(1,2);  //this變成了testO數組

 

咱們定義funcB函數的中,調用了funcA的call函數,這個時候咱們改變了funcA中this的指向,本來指向window的,如今指向了call的第一個參數testO這個對象。並且調用call時,由於funcA函數有兩個參數,因此若是要想funcA傳遞參數,必須一一指出參數,即後面的兩個參數a和b,或者能夠只穿第一個參數app

即:funcA.call(testO);或者只傳a,即:funcA.call(testO,a);async

而apply與call的區別僅在於,apply的第二個參數能夠是數組形式,並且沒必要一一指出參數,funcA.apply(testO,[a,b])ide

<script type="text/javascript">
        window.color = "透明";
        var testObj = { color: "紅色" };

        function testFuc() {
            alert(this.color);
        }函數

        $(function () {
            1.testFuc(); //彈出「透明」
            2.testFuc(this); //彈出「undefined」
            3.testFuc.call(this.parent); //彈出「透明」
            4.testFuc.call(window); //彈出「透明」
            5.testFuc.call(testObj); //彈出「紅色」
        });
</script>post

相關文章
相關標籤/搜索