jQuery $().each instead of $.each()
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?
I assume details
is an array, which is why details.each
is not going to work. You'll need to convert details
to a jQuery.init
object (the map of jQueried objects), which is done simply by throwing it into the jQuery
function:
return $( $(this).data('maps').details );
Are you forgetting the '#' or '.' to signify an id or class, respectively?
精彩评论