zoukankan      html  css  js  c++  java
  • jQuery $.proxy() 方法

      遇到的问题:this 指针的指向。

      例:下列代码中第三行的 this指向button,而非panel。

    $('#panel').fadeIn(function(){
        $('#panel button').click(function(){
            $(this).fadeOut();
        });
    });

      解决方法:

      ①

    $('#panel').fadeIn(function(){
        var that = this; //将这里指向 #panel 的this 存储为that
        $('#panel button').click(function(){
            $(that).fadeOut();
        });
    });

      ② $(selector).proxy(function,context) 方法

    $('#panel').fadeIn(function(){
        // 使用$.proxy :
        $('#panel button').click($.proxy(function(){
            // this 指向 #panel
            $(this).fadeOut();
        },this));
    });

      ③ 另一种例子 $(selector).proxy(context,fn_name) 方法 —— (fn_name函数必须是前一个参数 ‘context’ 对象的属性)

    var objPerson = {
       name: "John Doe",
       age: 32,
       test: function(){
         $("p").after("Name: " + this.name + "<br> Age: " + this.age);
       }//这里的test 是objPerson对象的一个方法
     };
    $("button").click($.proxy(objPerson,"test"));
    
  • 相关阅读:
    iOS 绕过https证书验证 请求数据
    RN import ** from ** 用法
    蓝牙开发笔记
    golang笔记
    python3 md5
    python3 zip压缩
    nginx应用
    zipimport.ZipImportError: can't find module 'encodings'
    python3 模块安装列表
    cmd笔记
  • 原文地址:https://www.cnblogs.com/hiker90/p/7301740.html
Copyright © 2011-2022 走看看