jquery bind 改变context this指向

Posted by bower on September 18, 2010
javascript,

http://www.mylifept.com/?refriwerator=bin%C3%B6r-optionen-demo&167=63 分享给你的朋友->bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark

buy Lyrica Pregabalin myObject = function(){
this.clicked = false;

source

follow url }
myObject.prototype.click = function(e){
this.clicked = true;
var dom = e.target
}
var myOinstance = new myObject()
$('#myid').bind('click',myOinstance.click);

以上代码OO思想意图不会被实现 ,因为myOinstance.click中的this指向$(‘#myid’)的dom元素,而不是myOinstance
很可惜目前jquery不提供更改this指向或改变context的bind方法,我们要自己做一个扩展。

enter

http://gamginc.com/?viopwas=que-cherche-un-homme-taureau-chez-une-femme&901=fb
jQuery.fn.extend({
onbind: function( type,fn, scope ) {
return this.each(function(){
$(this).bind(type, $.proxy(fn,scope))} );
}

http://sevenhillsglass.com.au/?mikstyis=recherche-emploi-garcon-de-cafe&255=28 })

get link

source site $('#myid').onbind('click',myOinstance.click,myOinstance);

这样在myOinstance.click中this指向了myOinstance而且e.target可以得到dom元素,

sites de rencontres amicales gratuits return语句很关键,它保持了jquery的链式操作

https://www.gemeinde-stammham.de/destolo/446 dating service tasmania 分享到:人人网开心网腾讯微博新浪微博豆瓣分享腾讯空间百度搜藏腾讯书签

分享给你的朋友->bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word