通过each来遍历标签集合中的每个元素。each可以传入一个函数,该函数第一个参数是索引,第二个参数是元素内容,俩个参数是js类型。元素参数可以转换为jQuery类型。
<script>
$(function () {
$divs = $('div');
// index索引,ele内容。他们俩是js标签。可以转换成jQuery标签
$divs.each(function (index, ele) {
console.log(index);
console.log(ele);
console.log($(ele));
});
$divs.each(function (index, ele) {
$(this).click(function () {
if (index != 2) {
$(this).css({
'color': 'red'
});
}
});
});
});
</script>