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

发布于 2024-12-27 07:22:50 字数 603 浏览 2 评论 0原文

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 在第一个示例中有效,但在第二个示例中无效。我需要 $.proxy,所以我无法摆脱它。一旦上下文在函数内部发生更改,我就找不到让 this.offsetLeft 工作的方法。

PS:由于我正在开发扩展,我无法再继续使用 self_ref[index]

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 works in the first example but not in the second. I need $.proxy, so I can't get rid of it. I can't find a way to get this.offsetLeft to work once the context has been changed inside it's function.

PS: I can no longer go back to using self_ref[index] because of an extensions I'm working on.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

桃扇骨 2025-01-03 07:22:50

要么像 Dennis 所说的那样在外部作用域中保留 this 的副本,要么使用 e.currentTarget 来获取正在处理事件的 DOM 元素。

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));

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

Either keep a copy of this in the outer scope like Dennis says, or use e.currentTarget to get the DOM element that is handling the event.

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));

in your first example (non proxied): this == e.currentTarget

浅听莫相离 2025-01-03 07:22:50

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

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

this in the first example refers to the element that was clicked. In the second, you are using it both to refer to the element that was clicked, as well as the object you are proxying the function to. You can modify the original, working example by renaming this when you create the handler and creating a closure:

var self = this;
this.progress.click(function(e) {
    var seek = (e.pageX - this.offsetLeft) / $(this).width();
    self.seek(seek * self.source.duration);
});
柏林苍穹下 2025-01-03 07:22:50

编辑:其他两个答案显然都比这个好得多。尊重:-)


this是被单击的DOMElementthis.progress

在第一个工作示例中 第二个示例,当使用 $.proxy 时,this 引用周围上下文,而不是 progress 元素。

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

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

而不是this.offsetLeft

Edit: both other answers are obviously far way better that this one. Respect :-)


In the first working example, this is the DOMElement that was clicked: this.progress

In the second example, when using $.proxy, this refers to the surrounding context, not the progress element anymore.

So within the proxified handler, you should be using:

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

instead of this.offsetLeft.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文