jQuery使用each方法与for语句遍历数组示例
本文实例讲述了jQuery使用each方法与for语句遍历数组。分享给大家供大家参考,具体如下:
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <htmlxmlns="http://www.w3.org/1999/xhtml"> <head> <metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/> <scriptsrc="jquery-1.6.2.js"type="text/javascript"></script> <scripttype="text/javascript"> vararray=["huangbiao","24","boy"]; varlength=array.length; functionforFunc(){ varresult=""; for(vari=0;i<length;i++){ result=result.concat(array[i]+";"); } $("#forFunc").html(result); returntrue; } functioneachFunc(){ varresult=""; $(array).each(function(i,n){ //下面两种方式实现的效果是一样的 //result=result.concat(array[i]+";"); result=result.concat(n+";"); }); $("#eachFunc").html(result); } </script> <title>无标题文档</title> </head> <body> <buttononclick="forFunc()">forFunc</button><br> <divid="forFunc"></div> <buttononclick="eachFunc()">eachFunc</button><br> <divid="eachFunc"></div> </body> </html>
更多关于jQuery相关内容感兴趣的读者可查看本站专题:《jQuery常用插件及用法总结》、《jquery中Ajax用法总结》、《jQuery表格(table)操作技巧汇总》、《jQuery拖拽特效与技巧总结》、《jQuery扩展技巧总结》、《jQuery常见经典特效汇总》、《jQuery动画与特效用法总结》及《jquery选择器用法总结》
希望本文所述对大家jQuery程序设计有所帮助。