同java中的for循环类似,传入一个数组或集合就可以遍历出其属性
var arr1 = [ "one", "two", "three", "four", "five" ]; $.each(arr1, function(){ alert(this); }); //输出:one two three four five
var arr2 = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] $.each(arr2, function(i, item){ //i为二维数组下标,item为当前下标对应的值 alert(item[0]); }); //输出:1 4 7
var obj = { one:1, two:2, three:3, four:4, five:5 }; $.each(obj, function(key, val) { //key为json下标,通过key取出value值 alert(obj[key]); }); //输出:1 2 3 4 5
语法:$(selector).each(function(index,element))
selector:jquery选取的元素 index:选择器index的位置 element:当前的元素 范例:
//遍历所有name属性为"ch"的input元素 $(“input[name=’ch’]”).each(function(i){ if($(this).attr(‘checked’)==true) { //一些操作代码 } }
如对本文有疑问, 点击进行留言回复!!
荐 精读Javascript系列(三) 执行上下文、 执行栈、初识事件循环
荐 JavaScript5种对象的命名属性创建(含测试源码)
在django中使用bootstrap4报错:OSError: [WinError 123] 文件名、目录名或卷标语法不正确。: ‘「frozen importlib._bootstrap」‘”
网友评论