遍历obj对象: 2016-5-20
var obj={ a:1, b:2, c:3 } for(var item in obj){ console.log(item); }
用 $.each 遍历数据: 2016-5-25
var data={ a:1, b:2, c:3, d:4, e:5 } $.each(data,function(key,val){ console.log(key+'-------'+val); });
用 $('ul li').each() 遍历DOM对象: 2016-5-25
//遍历 $("ul li").each(function(){ console.log($(this).text()) }); $("ul li").each(function(i,element){ console.log($(this).text()) console.log($(element).text()); console.log(i); });
遍历表单文本框:
<script type="text/javascript" src="http://cdn.bootcss.com/jquery/1.11.2/jquery.min.js"></script> <script type="text/javascript"> $(function(){ $("#button").click(function(){ //遍历表单文本框 $("#form1 input[name]").each(function(){ $(this).bind("blur",function(){ testtest($(this)); });//注意blur事件写法! function testtest(_this) { console.log(_this.val()); } }) }); }); </script> <form method="post" action="" id="form1"> <input type="text" name="a1" value="a111"><br> <input type="text" name="a2" value="a222"><br> <input type="text" name="a3" value="a333"><br> <input type="text" name="a4" value="a444"><br> <input type="button" value="button" id="button"> </form>
...