반복문으로 jQuery에서 많이 사용됩니다. 이유는 있는 만큼 반복한다는 장점과
Index에서 자동으로 반복 횟수가 저장됩니다.
01.Each.htm |
<html xmlns="http://www.w3.org/1999/xhtml"> <head>
<title>Each() 요소만큼 반복해서 속성 설정 및 가져오기</title>
<script src="../jQuery/jquery-1.3.2-vsdoc2.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function()
{
//[1] each() 메서드 설명 //$('p').each( function (index) { alert(index); });
//[2] for문처럼 반복한다. index는 0부터 자동 증가됨 $('p').each(//p 태그 만큼 반복 function(index)
{ $(this).attr({ // attr({어트리뷰트:값}); 'id': "para-"
+ index //ID 속성 동적으로 부여 }); }); //[3] 0번째 요소의 텍스트 읽어오기 $('#btn').click(function() {
alert($('#para-2').text()); //text() :
태그안의 값 : ASP.NET });
});
</script> </head> <body>
<p>C#</p>
<p>ASP.NET</p>
<p>Silverlight</p>
<input type="button" id="btn" value="동적으로 생성된 id로 개체 접근" />
</body> </html> |
결과화면 |
[그림21-1] |
'jQuery | javascript | CSS' 카테고리의 다른 글
23.jQuery - Input ( 02.Selectors - 01.Input ) (0) | 2009.11.09 |
---|---|
22.jQuery - For문 ( 01.Core- 02.Indexer ) (2) | 2009.11.09 |
-- 현재 까지의 jQuery 소스 -- (0) | 2009.11.09 |
20.jQuery - CallBack ( 20.CallBack ) (0) | 2009.11.09 |
19.jQuery - Browser ( 19.Browser ) (0) | 2009.11.09 |
Comments