jQuery 之$.proxy() 方法
定义和用法
$.proxy 方法接受一个已有的函数,并返回一个带特定上下文的新的函数。
该方法通常用于向上下文指向不同对象的元素添加事件。
参数 | 描述 |
---|---|
function | 要被调用的已有的函数。 |
context | 函数所在的对象的名称。 |
name | 已有的函数,其上下文将被改变(应该是 context 对象的属性)。 |
具体实例1:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
var initItem = function () {
this.$item = $("<div style='width:200px;height:100px;background:#ccc;'></div>");
this.initClick = function () {
var that = this; //这个this指的是initItem
// this.$item.click(function () {
// alert($(this).css("width")); //这个this指的是item,结果:200px
// that.aa();//alert(2)
// }); // this.$item.click($.proxy(function () {
// this.aa();//结果alert(2);
// }, this)); //这个this指的是initItem var o = {
name: "wowoowwo",
test: function () {
alert(this.name);
}
};
// this.$item.click($.proxy(bb.test, bb));
this.$item.click($.proxy(o, "test"));//$.proxy()用这个代理可以访问对象o里面的私有name
};
this.appendH = function () {
$(".main").append(this.$item);
};
this.init = function () {
this.initClick();
this.appendH();
};
this.aa = function () {
alert();
};
this.init();
}
initItem();
})
</script>
</head>
<body>
<div class="main"></div>
</body>
</html> 实例2:
$(function () {
var F = function () {
this.fClick = function () {
alert();
}
// $("#father").click(function () {
// this.fClick();//报错,this指向$("#father"),还没定义fClick方法
// }); $("#father").click($.proxy(function () {
//alert(1);
this.fClick(); ////0,this指向F,已定义fClick方法
}, this));
this.init = function () {
this.fClick();
}
this.init();
}
F();
})
jQuery 之$.proxy() 方法的更多相关文章
- jQuery的delegate()与proxy()方法
1. jQuery 事件 - delegate() 方法 定义和用法 delegate() 方法为指定的元素(属于被选元素的子元素)添加一个或多个事件处理程序,并规定当这些事件发生时运行的函数. 使用 ...
- JQuery之proxy实现绑定代理
在javascript中,this指代的对象时常会变化,这会造成程序,混乱,一般做法就是先将this保存在一个变量中,就不怕她变了,我们先看一个小例子 var A = function(){ this ...
- jQuery中$.proxy()的原理和使用
jQuery.proxy(),接受一个函数,然后返回一个新函数,并且这个新函数始终保持了特定的上下文(context )语境. jQuery.proxy( function, context ) fu ...
- jQuery常用工具方法
前面的话 jQuery提供一些与元素无关的工具方法,不必选中元素,就可以直接使用这些方法.如果理解原生javascript的继承原理,那么就能理解工具方法的实质.它是定义在jQuery构造函数上的方法 ...
- jq源码解析之绑在$,jQuery上面的方法
1.当我们用$符号直接调用的方法.在jQuery内部是如何封装的呢?有没有好奇心? // jQuery.extend 的方法 是绑定在 $ 上面的. jQuery.extend( { //expand ...
- jquery 通过submit()方法 提交表单示例
jquery 通过submit()方法 提交表单示例: 本示例:以用户注册作为例子.使用jquery中的submit()方法实现表单提交. 注:本示例仅提供了对表单的验证,本例只用选用了三个字段作为测 ...
- jquery.on()超级方法
$.on()方法是jquery1.7之后的一个超级方法,将事件绑定和事件委托整合到一个函数中去,支持绑定多个事件,并且可以绑定自定义事件.使用起来很方便. demo传送门 事件委托 首先说一下事件委托 ...
- 重写jquery的ajax方法
//首先备份下jquery的ajax方法 var _ajax=$.ajax; //重写jquery的ajax方法 $.ajax=function(opt){ //备份opt中error和success ...
- jQuery的extend方法
jq中的extend在面试中经常会被问道,今天我总结一个下有关于extend的用法三种进行对比,可能不全,希望大家指点, 用法一: $.extend({}) ,为jQuery类添加方法,可以理解为扩 ...
随机推荐
- monkeyrunner总结
device=MonkeyRunner.waitForConnection() //手机连接 result = device.takeSnapshot() //截图 result.write ...
- Android实现图片放大缩小
package com.min.Test_Gallery; import android.app.Activity; import android.graphics.Bitmap; import an ...
- Netmon: A light-weight network monitor for Windows
Netmon is a light-weight network monitor that works on Windows operating systems. It provides differ ...
- JSON格式的各种转换
/** *JSON 格式的解析 */ // json 去掉转义字符 message = message.replaceAll("\\\\", ""); //转成 ...
- .Net HttpClient 模拟登录微信公众平台发送消息
1.模拟登录 public WeiXinRetInfo ExecLogin(string name, string pass) { CookieContainer cc = new CookieCon ...
- 常见Oracle数据库问题总结及解决办法(一)
开发中常使用Oralce数据库,使用中也许会碰到形形色色的各类错误提示,如:ORA-00933:SQL命令未正确结束.ORA-009242等等,为此记录积累对于自己来说还是很有帮助的,今天就记录以前出 ...
- 实现类似QQ的折叠效果
// 主要核心是点击自定义header来展开和收起每一组里面的cell,模型里面应该有isShow此属性来记录开展还是收起. // ViewController.m// 实现类似QQ的折叠效果/ ...
- PHP 之mysql空字符串问题
有一张user表如下所示:字段name不能为空. CREATE TABLE `user` ( `id` ) NOT NULL AUTO_INCREMENT, `name` ) NOT NULL, `a ...
- Android系统服务-简介
http://blog.csdn.net/chenyafei617/article/details/6577907 Introduction 我们知道Android系统服务挺多的,做程序时经常会用到, ...
- cf C. Valera and Elections
http://codeforces.com/contest/369/problem/C 先见边,然后dfs,在回溯的过程中,如果在这个点之后有多条有问题的边,就不选这个点,如果没有而且连接这个点的边还 ...