How to properly use the each() in jQuery?
I have the following:
<div class="io-section-header">
<ul>
<li class="advanced">Eat</li>
<li class="advanced">Sleep</li>开发者_JAVA技巧;
<li class="advanced">Be merry</li>
</ul>
</div>
I have the jQuery (which is tied to a click() handler:
$('io-section-header').each(function() {
$("li").siblings(".advanced").toggle('fast',function(){});
});
Why aren't the list items toggling?
I dont think you actually need the each()
statement for what you are trying to do. This should work fine.
$('.io-section-header li').siblings(".advanced").toggle('fast');
here is a fiddle http://jsfiddle.net/8kpcn/1/
In fact this will do the same thing as well
$('.io-section-header li.advanced').toggle('fast');
http://jsfiddle.net/8kpcn/2/
You just need to add a dot before your class selector.
$('.io-section-header').each(function() {
$("li").siblings(".advanced").toggle('fast',function(){});
});
jsFiddle
Try this:
$('.io-section-header li').each(function() {
$(this).siblings(".advanced").toggle('fast');
});
jsFiddle
The callback function needs some parameters.
$('.io-section-header').each(function(index, Element) {
//Do whatever you need to do with each of them here.
//The current element in this iteration is in `Element`
});
精彩评论