jQuery $().each 而不是 $.each()
我正在使用 jQuery 的标准创作模式编写一个插件,使用方法来做不同的事情,但我遇到了 .each() 的问题。
我从文档中了解了 $.each()
和 $(selector).each()
之间的区别,但我不知道如何克服我的问题。 ..
在我的代码中,我有一个方法返回一个使用 $(selector).data()
存储的对象,我可以像这样运行 $.each() :
$.each($(selector).maps('getdetails'), function(key, value){/* stuff here */});
但是,在jQuery 可链接性的范围,这不是应该的样子,我无法理解如何做到这一点:
$(selector).maps('getdetails').each(function(key, value){/* stuff here */});
为了可链接性和用户的易用性,后者更好。但我总是收到以下错误:
TypeError: $(selector).maps("getdetails").each is not a function
这是变量对象中的相关方法:
getdetails: function(){
return $(this).data('maps').details;
},
(是的,在你说之前,我知道这是不正确的 JS,但正如我所说,它正确地包装在变量中{} 在我的代码中。)
有谁知道如何将此对象操作为后一个 .each()
函数将迭代的 jQuery 对象?
I am writing a plugin using the standard authoring pattern for jQuery, using methods to do different things with, and I'm running into a problem with .each().
I understand from the docs about the difference between $.each()
and $(selector).each()
yet I can't figure out how to overcome my problem...
In my code I have a method that returns an object which has been stored using $(selector).data()
which I can run a $.each() on like so:
$.each($(selector).maps('getdetails'), function(key, value){/* stuff here */});
However, in the scope of jQuery chainability, this isn't how it should be, and I can't understand how to do it like this:
$(selector).maps('getdetails').each(function(key, value){/* stuff here */});
For chainability and ease of use for the user, the latter is preferable. Yet I always get the following error:
TypeError: $(selector).maps("getdetails").each is not a function
Here is the method in question from within the variable object:
getdetails: function(){
return $(this).data('maps').details;
},
(Yes, before you say it, I know this is incorrect JS, but as I said, it is correctly wrapped in a variable {} in my code.)
Does anyone know how I can manipulate this object into a jQuery object that the latter .each()
function will iterate over?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我假设
details
是一个数组,这就是为什么details.each
不起作用。您需要将details
转换为jQuery.init
对象(jQueried 对象的映射),只需将其放入jQuery
即可完成此操作。代码>功能:I assume
details
is an array, which is whydetails.each
is not going to work. You'll need to convertdetails
to ajQuery.init
object (the map of jQueried objects), which is done simply by throwing it into thejQuery
function:您是否忘记了“#”或“.”分别表示 id 或 class?
Are you forgetting the '#' or '.' to signify an id or class, respectively?