0.总述

for循环

$.each

for in

1.简述

$.each(object,function(index,e){  ...  });    

object --> 需要遍历的对象或数组
index  --> 索引    
e      --> 循环的每个元素

2.代码

<!DOCTYPE html>
<html>
<body>
<ul id="ul">
	<li>1</li>
	<li>2</li>
	<li>3</li>
	<li>4</li>
</ul>
</body>
</html>


<script type="text/javascript" src="js/jquery/jquery-1.11.2.js"></script>
<script type="text/javascript">

	$(function(){

		var lis = $("#ul li");
		
		console.debug("方式①:使用普通for循环   ");
		for(var i=0;i<lis.length;i++){
			var li = lis[i];
			var value = $(li).html();
			console.debug(value);
		}
		
		
		console.debug("方式②:使用jquery方式循环");
		$.each(lis,function(index,obj){  //index:索引obj:循环的每个元素
			var value = $(obj).html();
			console.debug(value);
		}); 
		
		console.debug("方式②:使用jquery方式循环");
		lis.each(function(index,obj){
			var value = $(obj).html();
			console.debug(value);
		});
	});

</script>

3.for in

Logo

基于 Vue 的企业级 UI 组件库和中后台系统解决方案,为数万开发者服务。

更多推荐