개발일지/JavaScript + jquery
[jquery] each() 명령어
연습용365
2021. 9. 13. 17:49
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(경고)창에 순서대로 고양이, 진돗개, 호랑이, 사자가 뜬다.