each() 선택한 여러 개의 요소들에게 각각 순차적으로 접근을 할 때 사용한다.
기본형 $(요소선택).each(function(매개변수1, 매개변수2){ ... } );
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script src="../jquery-3.6.0.min.js"></script>
<script>
$(function(){
$('.animal').each(function(){var name = $(this).text();
alert(name);});
//each() 각각을 해주는데..선택된 이것의 텍스트를 alert화면에 뿌려줘
});
</script>
</head>
<body>
<div class="animal">고양이</div>
<div class="animal">진돗개</div>
<div class="animal">호랑이</div>
<div class="animal">사자</div>
</body>
</html>
$('.animal').each(function(){var name = $(this).text(); alert(name);});
$('.animal').each
==> 애니멀 클래스에 순차적으로 접근한다.
var name = $(this).text();
==> 변수 네임은 애니멀의 텍스트(고양이, 진돗개 등..)
alert(name);
==>네임의 값을 alert 화면에 뿌려줘
그러니 alert(경고)창에 순서대로 고양이, 진돗개, 호랑이, 사자가 뜬다.
'개발일지 > JavaScript + jquery' 카테고리의 다른 글
[jquery] radio 입력 받아서 출력하기 (0) | 2021.09.14 |
---|---|
[jquery] 변수명을 설정해서 쉽게 부르기(feat.alert) (0) | 2021.09.14 |
JavaScript를 Jquery로 바꾸기 예제 (0) | 2021.09.13 |
[jquery] clickTest 줄여서 쓰기 $(this) (0) | 2021.09.13 |
[jquery] focus()명령어 (0) | 2021.09.13 |