소프트웨어 이론교육/웹 프로그래밍 기본

jQuery API (3) - map

마루설아 2022. 8. 2. 10:33
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <h1>jQuery 실습 03</h1>
    <div id="box">box</div>
    <button id="changeBtn">Change</button>

    <script src="http://code.jquery.com/jquery.js"></script>
    <script>
        var fish = ['오징어', '꼴뚜기', '대구', '명태', '거북이'];
        var arr = fish.map(function(item, i, ar){
            //console.log(item, i, ar);
            return `<li>${item}</li>`;
        });

        $('#changeBtn').click(function(e){
            $('#box').append('<ul>');

            arr.forEach(function(item){
                $('#box ul').append(item);
            });

            $('#box').append('</ul>');
        });


    </script>
</body>
</html>