gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 03:50:45 25 4
gpt4 key购买 nike

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]

最佳答案

要么像 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

关于jQuery:在 $.proxy 内查找左侧偏移量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8883620/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com