jquery中可用“$.each()”方法来循环遍历数组元素,然后在循环体中将数组元素一一输出;语法“$.each(arr,function(index,value){console.log(index+":"+value);})”。

相关推荐:《jQuery视频教程》
本教程操作环境:windows7系统、jquery2.2.4版,该方法适用于所有品牌电脑。
在jquery中,可以使用$.each()方法来循环数组元素,实现输出全部数组元素。
html>
<meta>
<title>JQuery循环遍历输出数组元素示例</title>
<script></script><script>
$(document).ready(function(){
var arr=["Apple", "Banana", "Orange", "Mango", "Pineapple"];
$.each(arr, function(index, value){
console.log(index + ": " + value + '<br>');
});
});
</script>
$.each()方法中有两个参数,第一个参数,即要循环的数组名称,第二个参数则是一个方法。
这里我们定义了一个arr数组,然后在此方法中每次回调就传递一个数组索引(index)和相应的数组值(value),再通过append() 方法将循环输出指定数组值的结果打印出来。
jQuery 1.12.4是jQuery 1.x系列的最后一个正式稳定版本,由jQuery团队于2016年发布。该版本主要面向需要兼容旧版浏览器环境的网站和Web应用,尤其适用于仍需支持Internet Explorer 6、Internet Explorer 7、Internet Explorer 8等老旧浏览器的项目。
结果如下所示:

更多编程相关知识,请访问:编程视频课程!!










