jQuery 之$.proxy() 方法

定义和用法

$.proxy 方法接受一个已有的函数,并返回一个带特定上下文的新的函数。

该方法通常用于向上下文指向不同对象的元素添加事件。


参数描述
function要被调用的已有的函数。
context函数所在的对象的名称。
name已有的函数,其上下文将被改变(应该是 context 对象的属性)。

 

 

 

具体实例1:

 1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 2 <html xmlns="http://www.w3.org/1999/xhtml">
 3 <head>
 4     <title></title>
 5     <script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
 6     <script type="text/javascript">
 7         $(function () {
 8             var initItem = function () {
 9                 this.$item = $("<div style='width:200px;height:100px;background:#ccc;'></div>");
10                 this.initClick = function () {
11                     var that = this; //这个this指的是initItem
12 //                    this.$item.click(function () {
13 //                        alert($(this).css("width")); //这个this指的是item,结果:200px
14 //                        that.aa();//alert(2)
15 //                    });
16 
17 
18 //                    this.$item.click($.proxy(function () {
19 //                        this.aa();//结果alert(2);
20 //                    }, this)); //这个this指的是initItem
21 
22                     var o = {
23                         name: "wowoowwo",
24                         test: function () {
25                             alert(this.name);
26                         }
27                     };
28                     // this.$item.click($.proxy(bb.test, bb));
29                     this.$item.click($.proxy(o, "test"));//$.proxy()用这个代理可以访问对象o里面的私有name
30                 };
31                 this.appendH = function () {
32                     $(".main").append(this.$item);
33                 };
34                 this.init = function () {
35                     this.initClick();
36                     this.appendH();
37                 };
38                 this.aa = function () {
39                     alert(2);
40                 };
41                 this.init();
42             }
43             initItem();
44         })
45     </script>
46 </head>
47 <body>
48 <div class="main"></div>
49 </body>
50 </html>

实例2:
 1  $(function () {
 2             var F = function () {
 3                 this.fClick = function () {
 4                     alert(0);
 5                 }
 6                 //                 $("#father").click(function () {                                     
 7                 //                    this.fClick();//报错,this指向$("#father"),还没定义fClick方法
 8                 //                });
 9 
10                 $("#father").click($.proxy(function () {
11                     //alert(1);                   
12                     this.fClick(); ////0,this指向F,已定义fClick方法
13                 }, this));
14                 this.init = function () {
15                     this.fClick();
16                 }
17                 this.init();
18             }
19             F();
20         })

 

 

 

转载于:https://www.cnblogs.com/dean-Wei/p/3966770.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值