22.jquery的遍历操作

56 阅读1分钟

1.关于jquery的遍历操作,each方法

	$( function(){
				// 1.遍历button 相邻的元素
			 // 1.1 使用工具函数
		 //	 $.each(  $("button").siblings() ,function(index ,item ){
		//		   console.log(item) ;
		//	 }		  )
			// 2.使用成员函数each
			$("button").siblings().each( function( index,item ){
				console.log(item) ;
			} )
				
				
			} )
			

使用 each() 方法可以遍历指定的元素集合,在遍历时,通过回调函数返回当前遍历元素的索引和当前元素对象,它的调用格式为:

$(selector).each(function(index,element))
  • 参数 function 为遍历时的回调函数
  • index 为遍历元素的索引,从 0 开始
  • element 为当前的元素对象(也可使用 "this" 选择器)