¾È±Ô °øºÎ¹æ

Javascript & JQuery & CSS > each() - ¹è¿­°ú °´Ã¼¸¦ ¼øȸÇÒ ¸ñÀûÀÇ ÇÔ¼ö

µî·ÏÀÏ : 2017-07-04 20:10 Á¶È¸¼ö : 53,555

1. each()
$.each(¹è¿­°´Ã¼, function(index,item){});

¹è¿­°´Ã¼ÀÇ ¸ðµç ¿ä¼Ò¿¡¼­ functionÀ» ¼öÇàÇÏ°í index¿¡´Â ¹è¿­ÀÇ À妽º°¡ Àü´ÞµÇ°í item¿¡´Â ¹è¿­ÀÇ µ¥ÀÌÅÍ°¡ Àü´ÞµË´Ï´Ù.

¹è¿­°´Ã¼°¡ ¾Æ´Ñ ÀϹݰ´Ã¼¸¦ Àû¿ëÇÏ¸é °´Ã¼ ¸ðµç ¼Ó¼º°ú °ªÀÌ Àü´ÞµË´Ï´Ù.
 

			var str = '';
			var ar = [{name:'sun', team:'KIA'},
			   {name:'ryu', team:'SAMSUNG'},
			   {name:'lee', team:'SK'},
			   {name:'yang', team:'LOTTE'},
			   {name:'KIM', team:'DUSAN'}];
	   
			$.each(ar, function(index, item) {
				str +=item.name+' : '+item.team + "\n\r";
			});

			alert(str);

 À§ ¿¹Á¦¸¦ º¸¸é ar ¹è¿­°´Ã¼¿¡ µ¥ÀÌÅ͸¦ ³Ö¾ú½À´Ï´Ù. 

ar = { name:'sun',team:'KIA' } , {name:'ryu', team:'SAMSUNG'} ,...

ar Àº ¹è¿­°´Ã¼°¡ µÇ°í, µ¥ÀÌÅÍ´Â name = 'sun' , team = 'KIA' °¡ µË´Ï´Ù. ÀÌ·±°æ¿ì ¹è¿­À» óÀ½ºÎÅÍ ³¡±îÁö ¼øÂ÷ÀûÀ¸·Î

Ž»öÇÏ°íÀÚ ÇÒ¶§ »ç¿ëÇÏ´Â ¸Þ¼Òµå´Â each ÀÔ´Ï´Ù.


$.each(ar, function(index, item) { /* 󸮷ÎÁ÷ */ });

each() ¸Þ¼Òµå´Â À§¿Í °°Àº ÇüÅ·Π»ç¿ëÀÌ °¡´ÉÇÕ´Ï´Ù.

ù¹ø° ÀÎÀÚ ar Àº ¹è¿­°´Ã¼°¡ µé¾î°¡°í , µÎ¹ø° ÀÎÀÚ¿¡´Â function() ÀÌ ¼öÇàÀÌ µÇ´Âµ¥¿ä,

À̶§ ÀÎÀÚ°ª(index , item) ÀÌ µÎ°³°¡ ³Ñ¾î¿À°Ô µÇ´Âµ¥, index ´Â ¹è¿­ÀÇ index °ªÀÌ µÇ°í, item Àº ¹è¿­ÀÇ µ¥ÀÌÅÍ(name, team)°¡ µË´Ï´Ù.

µ¥ÀÌÅÍ Ãâ·ÂÀº item.name , item.team °ú °°Àº Çü½ÄÀ¸·Î µ¥ÀÌÅÍ °ªÀ» »ÌÀ» ¼ö°¡ ÀÖ½À´Ï´Ù.

¹öÆ°À» ´­·¯ º¸½Ã¸é ¾Æ½Ã°ÚÁö¸¸, ¹è¿­¿¡ ÀÖ´Â µ¥ÀÌÅÍ°¡ alert À¸·Î Ç¥½Ã°¡ µÇ´Â°É È®ÀÎ ÇÒ ¼ö ÀÖ½À´Ï´Ù.


±×·³, index ¸¦ ÀÌ¿ëÇÑ µÎ¹ø° Å×½ºÆ® ¼Ò½º¸¦ º¸½Ã°Ú½À´Ï´Ù.
 

			var str = '';
			$('li').each(function(index, item){
				$(item).css('font-size',(index+1)*8);
			});


  Ŭ¸¯ÇØ º¸½Ã¸é AAA,BBB,CCC,.... ³»¿ëÀÌ <li> </li> ÅÂ±× ¾È¿¡ ÀԷµǾî ÀÖ½À´Ï´Ù.

each() ÇÔ¼ö¸¦ ÀÌ¿ëÇؼ­ li °´Ã¼¸¦ ¼øÂ÷ÀûÀ¸·Î Á¢±ÙÇÏ¿© item(li) °ª css ¸¦ º¯°æÇÏ°Ô µË´Ï´Ù.

index :0 index : 1 , index :2 °ªÀ¸·Î º¯°æÀÌ µÇ¾î function À» ¼öÇàÇÏ°Ô µÇ°ÚÁö¿ä????

ÀÌ°ÍÀ¸·Î each ÇÔ¼ö¸¦ ÀÌ¿ëÇؼ­ °´Ã¼¿¡ Á¢±ÙÇÏ´Â ¹æ¹ý¿¡ ´ëÇؼ­ ¾Ë¾Æ º¸¾Ò½À´Ï´Ù.
 
¡Ø Ȥ½Ã µµ¿òÀÌ µÇ¼Ì´Ù¸é ´ñ±Û¿¡ ÇѸ¶µð ³²°ÜÁÖ¼¼¿ä!
ÀÛ¼ºÀÚ   ºñ¹Ð¹øÈ£
ÀÚµ¿±Û ¹æÁö     (ÀÚµ¿±Û ¹æÁö ±â´ÉÀÔ´Ï´Ù.)
³»¿ë   ´ñ±Û´Þ±â 
À̸ÞÀÏ ¹®ÀÇ : cak0280@nate.com  
Copyright 2000 By ENTERSOFT.KR All Rights Reserved.