gpt4 book ai didi

javascript - jQuery 插件 : how to pass element reference to callback function?

转载 作者:行者123 更新时间:2023-11-29 18:03:17 25 4
gpt4 key购买 nike

TL;DR:我有 jQuery.myPlugin

$.fn.myPlugin = function(param, callback){
//...which does some operations to each member of collection
//obtained by $('.someclass').myPlugin()
//that are represented with this variable
}

当插件的工作完成时,如何将 this 变量 - 单节点引用 - 传递给 callback?像这样:

$.fn.myPlugin = function(param, callback){
//...
//...
//when job is done:
callback.call(this);
}
$('.someclass').myPlugin(options, function(arg){
//I need arg to be this variable from plugins definition...
})

只是提一下,无论我传递给 callback.call(somevar),somevar 在执行的匿名回调函数中都不可用。

最佳答案

如果回调确实是一个函数,你应该试试

callback(this);

关于javascript - jQuery 插件 : how to pass element reference to callback function?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33425592/

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