<html lang="ko">
<head>
<meta charset="UTF-8">
<title>jQuery Filtering</title>
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script>
$(function() {
$("button").on("click", function() {
// 선택한 <li>요소마다 콜백함수를 실행하여 각 <li>요소의 id 값을 반환함.
var ids = $("li").map(function() {
return this.id;
})
.get() // 반환된 모든 id 값을 하나의 배열로 반환함.
.join(); // 배열의 모든 요소를 쉼표(,)로 구분하는 하나의 문자열로 반환함.
$("#text").html(ids);
});
});
</script>
</head>
<body>
<h1>.map() 메소드</h1>
<ul>
<li id="one">첫 번째 아이템이에요!</li>
<li id="two">두 번째 아이템이에요!</li>
<li id="three">세 번째 아이템이에요!</li>
<li id="four">네 번째 아이템이에요!</li>
</ul>
<button>li 요소의 모든 id 값을 출력!</button>
<p id="text"></p>
</body>
</html>