I want to use jQuery UI's position utility to center a number of elements with the same class inside of their respective parent elements. Here's what I came up with:
$(".elementclass").position({
"my": "center center",
"at": "center center",
"of": $(this).paren开发者_StackOverflow中文版t()
});
Unfortunately, this does not work, since the jQuery object $(this) somehow does not refer to the positioned element in this context. How can I pull this off?
How about this:
$(".elementclass").each(function(i)
{
$(this).position({
"my": "center center",
"at": "center center",
"of": $(this).parent()
});
});
You could put it inside an "each" function like this:
$(".elementclass").each(function() {
$(this).position({
"my": "center center",
"at": "center center",
"of": $(this).parent()
});
});
This would loop through each element with the class of "elementclass", and position each item individually. Because you're referencing each element inside the function, $(this) will refer to the element you're trying to position.
精彩评论