Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jQuery:在$.proxy中查找左偏移量_Jquery_Proxy_Offset - Fatal编程技术网

jQuery:在$.proxy中查找左偏移量

jQuery:在$.proxy中查找左偏移量,jquery,proxy,offset,Jquery,Proxy,Offset,此.offsetLeft在第一个示例中有效,但在第二个示例中无效。我需要$.proxy,所以我无法摆脱它。我找不到一种方法来让this.offsetLeft在其函数中的上下文发生更改后工作 PS:我不能再使用self\u ref[index],因为我正在开发一个扩展。Edit:其他两个答案显然比这个好得多。尊敬:-) 在第一个工作示例中,this是单击的doElement:this.progress 在第二个示例中,当使用$.proxy时,此引用周围的上下文,而不再是进度元素 因此,在代理处理

此.offsetLeft
在第一个示例中有效,但在第二个示例中无效。我需要
$.proxy,所以我无法摆脱它。我找不到一种方法来让
this.offsetLeft
在其函数中的上下文发生更改后工作


PS:我不能再使用
self\u ref[index]
,因为我正在开发一个扩展。

Edit:其他两个答案显然比这个好得多。尊敬:-)


在第一个工作示例中,
this
是单击的doElement
this.progress

在第二个示例中,当使用
$.proxy
时,
引用周围的上下文,而不再是
进度
元素

因此,在代理处理程序中,您应该使用:

this.progress.click(function(e) { // works
    var seek = (e.pageX - this.offsetLeft) / $(this).width();
    self_ref[index].seek(seek * self_ref[index].source.duration);
});

this.progress.click($.proxy(function(e) { // doesn't work
    var seek = (e.pageX - this.offsetLeft) / $(this).width();
    this.seek(seek * this.source.duration);
}, this));

在第一个示例中,此
指的是单击的元素,而不是
this.offsetLeft

。在第二个示例中,您使用它来引用被单击的元素以及将函数代理到的对象。创建处理程序并创建闭包时,可以通过重命名
this
来修改原始的工作示例:

 this.progress.get(0).offsetLeft 
 // .get(0) is to obtain the DOMElement
 // and get access to the javascript property offsetLeft 

或者像Dennis所说的那样在外部范围中保留一份
this
,或者使用
e.currentTarget
获取处理事件的DOM元素

var self = this;
this.progress.click(function(e) {
    var seek = (e.pageX - this.offsetLeft) / $(this).width();
    self.seek(seek * self.source.duration);
});

在您的第一个示例中(非代理):
this==e.currentTarget

它不起作用,我尝试了几种组合以获得左侧
offsetLeft
offset()。left
left
css('offset')。left
。使用
this.progress.get(0).offsetLeft
来处理DomeElement(就像第一个示例中的
this
)谢谢,这正是我要找的。
this.progress.click($.proxy(function(e) { // doesn't work
    var seek = (e.pageX - e.currentTarget.offsetLeft) / $(e.currentTarget).width();
    this.seek(seek * this.source.duration);
}, this));