2008年9月4日 星期四

jquery each find what is different


[html]
<div id="'test'">
<span id="'each'">(click here to change)</span>
<ul>
<li class="cc">Eat</li>
<li class="cc">Sleep</li>
<li class="bb">Be merry</li>
</ul>
</div>

[script 1]
$("#test span").click(function () {
if ($("#test").find('li')) {
alert($(this).html());
}
});
$(this) is $(#test span)
[script 2]
$("#test span").click(function () {
if ($("#test li")) {
alert($(this).html());
}
});

$(this) is $(#test span)


[script 3]
$("#test span").click(function () {
$("#test li").each(function(i, e){
alert($(this).html());
}
});

$(this) is really every li tag
i is the iterator counter
e == $(this)



Returning 'false' from within the each function completely stops the loop through all of the elements (this is like using a 'break' with a normal loop). Returning 'true' from within the loop skips to the next iteration (this is like using a 'continue' with a normal loop).

沒有留言: