示例 2 : 去除重复 示例 3 : 是否存在$.inArray
$.each 遍历一个数组
第一个参数是数组 第二个参数是回调函数 i是下标,n是内容 <script src="https://how2j.cn/study/jquery.min.js"></script>
<script>
var a = new Array(1,2,3);
$.each( a, function(i, n){
document.write( "元素[" + i + "] : " + n + "<br>" );
})
document.close();
</script>
<script src="https://how2j.cn/study/jquery.min.js"></script> <script> var a = new Array(1,2,3); $.each( a, function(i, n){ document.write( "元素[" + i + "] : " + n + "<br>" ); }) document.close(); </script> <script src="https://how2j.cn/study/jquery.min.js"></script>
<script>
var a = new Array(5,2,4,2,3,3,1,4,2,5);
a.sort();
$.unique(a);
$.each( a, function(i, n){
document.write( "元素[" + i + "] : " + n + "<br>" );
})
document.close();
</script>
<script src="https://how2j.cn/study/jquery.min.js"></script> <script> var a = new Array(5,2,4,2,3,3,1,4,2,5); a.sort(); $.unique(a); $.each( a, function(i, n){ document.write( "元素[" + i + "] : " + n + "<br>" ); }) document.close(); </script>
$.inArray 返回元素在数组中的位置 ,如果不存在返回-1
<script src="https://how2j.cn/study/jquery.min.js"></script>
<script>
var a = new Array(1,2,3,4,5,6,7,8);
document.write($.inArray(9,a));
document.close();
</script>
<script src="https://how2j.cn/study/jquery.min.js"></script> <script> var a = new Array(1,2,3,4,5,6,7,8); document.write($.inArray(9,a)); document.close(); </script>
HOW2J公众号,关注后实时获知最新的教程和优惠活动,谢谢。
提问已经提交成功,正在审核。 请于 我的提问 处查看提问记录,谢谢
|