zoukankan      html  css  js  c++  java
  • 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添加静态方法。 

    $.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(arr.push("James") + "<br />")
    document.write(arr)
    

    输出:

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

            funcB(1,2);  //this变成了testO

    我们定义funcB函数的中,调用了funcA的call函数,这个时候我们改变了funcA中this的指向,原本指向window的,现在指向了call的第一个参数testO这个对象。而且调用call时,因为funcA函数有两个参数,所以如果要想funcA传递参数,必须一一指出参数,即后面的两个参数a和b,或者可以只穿第一个参数

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

    而apply与call的区别仅在于,apply的第二个参数可以是数组形式,而且不必一一指出参数,funcA.apply(testO,[a,b])

    <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>

  • 相关阅读:
    postgresql 简单入门
    自用 docker-compose
    前后端分离 开发环境通过CORS实现跨域联调
    导出 java.io.IOException: 权限不够
    mysql 数值与字符类型 长度梳理
    easyui input未设id导致的问题
    springmvc处理url请求步骤
    js中的变量提升(hoisting)
    auto semicolon insertion 自动分号补齐的坑
    $.ajax dataType设置为json 回调函数不执行
  • 原文地址:https://www.cnblogs.com/Zatsugaku-code/p/5761210.html
Copyright © 2011-2022 走看看