jQuery(attr)

๋…ธ๊ฑด์šฐยท2023๋…„ 8์›” 11์ผ
0

attrํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•œ ์†์„ฑ๊ฐ’ ์–ป์–ด์˜ค๊ธฐ

<!DOCTYPE html>
<html lang="ko">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no">
    <title>jQuery</title>
   
   
    <style>
        /* ์ „์ฒด ์—ฌ๋ฐฑ ์ œ๊ฑฐ */
        *  { margin: 0; padding: 0; }

        /* ๋ชฉ๋ก์ •์˜ ํƒœ๊ทธ */
        ul { list-style: none; }

        /* float ์ฒ˜๋ฆฌ์šฉ ํด๋ž˜์Šค ๋ฐ ๋งˆ๊ฐ์ œ ํด๋ž˜์Šค ์ •์˜ */
        .pull-left { float: left; }
        .clearfix:after { display: block; content: ''; clear: both; float: none; }
        
        /* ๋ฐ”๊นฅ์˜ ๋ฐ•์Šค */
        .preview-box { margin: auto; width: 470px; border: 3px solid #d5d5d5; padding: 10px; }

        /* ํฐ ์ด๋ฏธ์ง€ ์˜์—ญ */
        .preview { width: 100%; height: 470px; margin-bottom: 5px; }

        /* ๋ชฉ๋ก์ •์˜ ์•„์ดํ…œ ํฌ๊ธฐ, ์—ฌ๋ฐฑ์„ค์ •(์™ผ์ชฝ๋งˆ์ง„) */
        .item { width: 70px; height: 70px; margin-left: 10px; }

        /* ๋ชฉ๋ก์ •์˜ ์ฒซ ๋ฒˆ์งธ ํ•ญ๋ชฉ์€ ์™ผ์ชฝ๋งˆ์ง„ ์ œ๊ฑฐ */
        .item:first-child { margin-left: 0; }

        /* ์ž‘์€ ํฌ๊ธฐ์˜ ์ด๋ฏธ์ง€ ์‚ฌ์ด์ฆˆ ์„ค์ • ๋ฐ ๋งˆ์šฐ์Šค ์ปค์„œ ๋ชจ์–‘ ์„ค์ • */
        .thumbnail { width: 100%; height: 100%; cursor: pointer; }
    </style>
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.0/jquery.min.js"></script>

</head>

<body>
    <div class='preview-box'>
        <img src="img/captainamerica.png" class="preview" id="target" />
        <ul class="list clearfix">
            <li class='item pull-left'><img src="img/captainamerica.png" class='thumbnail' /></li>
            <li class='item pull-left'><img src="img/batman.png" class='thumbnail' /></li>
            <li class='item pull-left'><img src="img/spiderman.png" class='thumbnail' /></li>
            <li class='item pull-left'><img src="img/ironman.png" class='thumbnail' /></li>
            <li class='item pull-left'><img src="img/hulk.png" class='thumbnail' /></li>
            <li class='item pull-left'><img src="img/thor.png" class='thumbnail' /></li>
        </ul>
     
    </div>
    <script>
        //attr,$(this)
        $(".thumbnail").click(function(){
            let img = $(this).attr("src");
            //console.log(img)
            $("#target").attr('src',img);
        });


     
    </script>
  • ๋จผ์ € img๋ผ๋Š” ๋ณ€์ˆ˜๋ฅผ ์„ค์ •ํ•ด ์ฃผ๊ณ  $์— ์š”์†Œ๊ฐ’์„ ์ง‘์–ด๋„ฃ๊ณ  src์†์„ฑ ๊ฐ’์„ ๊ฐ€์ ธ์˜จ๋‹ค. ์†์„ฑ์˜ ์ด๋ฆ„์ด src์ด๊ณ  ๊ฐ’์€ image๋ฅผ ๋œปํ•˜๋ฏ€๋กœ ์œ„์—์„œ ์ •์˜ํ•ด์ค€ png์˜ ๊ฐ’์„ ๊ฐ€์ ธ์˜จ๋‹ค.

    โœ๏ธ ํฌ์ธํŠธ๋Š” ์š”์†Œ๊ฐ’, ์†์„ฑ๊ฐ’, ์†์„ฑ ์ด๋ฆ„๋“ฑ ํ•˜๋‚˜ํ•˜๋Š์ด ํŠน์ง•์„ ์•Œ๊ณ  ํ˜ธ์ถœํ•  ๋•Œ ๊ทธ๋“ค์˜ ๊ด€๊ณ„๋ฅผ ์•„๋Š” ๊ฒƒ์ด ํฌ์ธํŠธ๋‹ค. ์ฝ”๋“œ๊ฐ€ ์ปดํŒฉํŠธํ•˜๊ณ  ๊ธธ์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ์ฝ”๋“œ๋งŒ ๋ณด๋Š” ๊ฒƒ์ด ์•„๋‹Œ ๋กœ์ง์„ ๋ถ„์„ํ•ด์ฃผ์ž.

<!DOCTYPE html>
<html lang="ko">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no">
    <title>jQuery</title>
</head>

<body>
    <!-- ๋ฒ ๋„ˆ์˜์—ญ -->
    <div id="banner">
        <a href="#" id="banner-link" target="_blank">
            <img src="" id="banner-img" width="320" />
        </a>
    </div>
    
    <script src="http://code.jquery.com/jquery-3.2.1.min.js"></script>
    <script>
        let banner = [
            { img:'img/captainamerica.png', url:'https://www.naver.com' },
            { img:'img/batman.png', url:'https://www.google.com' },
            { img:'img/ironman.png', url:'https://www.daum.net' },
            { img:'img/thor.png', url:'https://www.google.com' }
        ];

        // ํŽ˜์ด์ง€๊ฐ€ ์—ด๋ฆฐ ํ›„ ํ˜ธ์ถœ๋  ํ•จ์ˆ˜
        $(function(){
            random_banner();

            // 1์ดˆ๋งˆ๋‹ค ๋ฐฐ๋„ˆ ๋ณ€๊ฒฝ์„ ์œ„ํ•ด์„œ ํƒ€์ด๋จธ ๊ฐ€๋™
            setInterval(function(){
                random_banner();
            }, 1000);
        });

        function random_banner(){
            let rand = random(0, banner.length-1);
            let item = banner[rand];
            // ๋ฐฐ๋„ˆ์— ์ ์šฉ
            $("#banner-img").attr('src',item.img);
            $("#banner-link").attr('href', item.url);
        }

        function random(n1, n2){
            return parseInt(Math.random() * (n2 - n1 + 1)) + n1;
        }
    </script>
</body>

</html>
  • ๋ฐฐ๋„ˆ๋ผ๋Š” ๋ฐฐ์—ด๊ฐ’์„ ๋งŒ๋“ค์–ด 4๊ฐ€์ง€์˜ ์ด๋ฏธ์ง€, ๊ทธ์— ๋”ฐ๋ฅธ url์„ ์„ค์ •ํ•ด ์ค€๋‹ค.
  • ๋จผ์ € ํŽ˜์ด์ง€๊ฐ€ ์—ด๋ฆฐ ํ›„ ํ˜ธ์ถœ๋  ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค๊ณ , ๊ทธ ํŽ˜์ด์ง€์˜ ์‚ฌ์ง„๋“ค์ด ๋žœ๋ค์œผ๋กœ ๋‚˜์˜ค๊ฒŒ ํ•˜๋Š” ๋กœ์ง ์ƒ์„ฑ
  • ๋‹ค์Œ์€ $,attr์„ ์ด์šฉํ•˜์—ฌ ๋ฐฐ๋„ˆ์— ์ ์šฉํ•˜๊ณ , ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•ด์ค€๋‹ค.

โœ๏ธ ์ด ์ฝ”๋“œ์—์„œ ์ฃผ๋ชฉํ•ด์•ผ ํ•  ์ ์€ ๋ฐฐ๋„ˆ์— ์ ์šฉํ•˜๋Š” ๊ณผ์ •์ด๋‹ค. banner-img๋Š” ๋ฐฐ๋„ˆ์˜ ์•„์ด๋””์— ์„ค์ •๋˜์–ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— #banner-img๋กœ ๊ฐ€๊ณ , ์†์„ฑ์ด๋ฆ„์ด src์ด๋ฉฐ ๋ฐฐ์—ด๋กœ ์ €์žฅ๋œ banner์˜ img๊ฐ’์„ ๊ฐ€์ ธ์˜จ๋‹ค.
banner-link๋Š” ์•„์ด๋”” ๊ฐ’์ด href์ด๋ฉฐ, ๋ฐฐ์—ด๋กœ ์ €์žฅ๋œ url๊ฐ’์„ ๊ฐ€์ ธ์˜จ๋‹ค.

ํƒœ๊ทธ์˜ ๊ธฐ๋Šฅ์ค‘๋‹จ

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.0/jquery.min.js"></script>
</head>
<body>
    <a href="http://www.naver.com" id = "link1">link1</a>
    <a href="http://www.naver.com" id = "link2">link2</a>

    <script>
        $('#link1').click(function(){
            alert("link1๋ฅผ ํด๋ฆญํ–ˆ์Šต๋‹ˆ๋‹ค.")
        })
        //aํƒœ๊ทธ์˜ href๊ธฐ๋Šฅ ์ค‘๋‹จ
        $('#link2').click(function(e){
            e.preventDefault();
            alert("link2๋ฅผ ํด๋ฆญํ–ˆ์Šต๋‹ˆ๋‹ค.")
        })
    </script>
</body>
</html>

๐Ÿ‘Š๐Ÿ‘Š์œ„์˜ ์ฝ”๋“œ์— ๋น„ํ•˜๋ฉด ๋‚œ์ด๋„๊ฐ€ ๋งŽ์ด ๋‚ฎ์€ ์‰ฌ์–ด๊ฐ€๋Š” ์ฝ”๋“œ์ด๋‹ค. ๋จผ์ € id๊ฐ€ link1์šธ ํด๋ฆญํ•˜๋ฉด "link1์„ ํด๋ฆญํ–ˆ์Šต๋‹ˆ๋‹ค."๋ผ๋Š” ์•Œ๋ฆผ์ฐฝ์ด ๋œจ๊ณ , ,naver๋กœ ์ ‘์†ํ•˜๊ฒŒ ๋œ๋‹ค. link2๊ฐ™์€ ๊ฒฝ์šฐ๋Š”preventDefault๋ฅผ ์ด์šฉํ•ด ๋งํฌ๋กœ์˜ ์ ‘์†์„ ๋ง‰์•„๋‘๊ณ  "link2๋ฅผ ํด๋ฆญํ–ˆ์Šต๋‹ˆ๋‹ค"์•Œ๋ฆผ์ฐฝ์„ ๋„์šฐ๊ฒŒ ๋œ๋‹ค.

profile
์ดˆ๋ณด ๊ฐœ๋ฐœ์ž ์ด์•ผ๊ธฐ

0๊ฐœ์˜ ๋Œ“๊ธ€