1. each()메소드 - 요소만큼 반복해서 속성 설정 및 가져오기
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html;" charset="utf-8" >
<title> 요소만큼 반복해서 속성 설정 및 가져오기 </title>
<script src="http://code.jquery.com/jquery-1.7.2.js"></script>
<style type="text/css">
.red{color:red;}
</style>
<script>
$(document).ready(function (){
//[1]index변수는 p태그의 index번호.
$('p').each(function(index){
$(this).attr({ //attr({attribute value})
'id':"para-"+index
});
});
$('#btn').click(function(){
alert($('#para-2').text()); //text():태그안의 값
});
});
</script>
</head>
<body>
<p>첫번째</p>
<p>두번째</p>
<p>세번째</p>
<p>네번째</p>
<input type="button" id="btn" value="button">
</body>
</html>
'강좌 > jQuery' 카테고리의 다른 글
제이쿼리(jQuery) - jQuery 선택기 설명 2 (패스워드 일치) (0) | 2013.04.22 |
---|---|
제이쿼리(jQuery) - jQuery 선택기(셀렉터, 실렉터, 선택자, Selector) 설명 1 (0) | 2013.04.22 |
제이쿼리(jQuery) - css() 메서드와 콜백함수(CallBack) (0) | 2013.04.22 |
제이쿼리(jQuery) - toggle(), toggleClass(), hover(), one() 메서드. (0) | 2013.04.22 |
제이쿼리(jQuery) - filter()와 slice() 메서드. (0) | 2013.04.22 |