JavaScript/๊ธฐ๋ณธ

this - jQuery/ javascript ์ฐจ์ด

dev_jiwon 2022. 6. 23.

https://inpa.tistory.com/entry/jQuery-%F0%9F%93%9A-%EC%9E%90%EB%B0%94%EC%8A%A4%ED%81%AC%EB%A6%BD%ED%8A%B8-this-%E2%9A%94%EF%B8%8F-%EC%A0%9C%EC%9D%B4%EC%BF%BC%EB%A6%AC-this#%F0%9F%93%8C_%EC%9D%B4%EB%B2%A4%ED%8A%B8_%ED%95%B8%EB%93%A4%EB%9F%AC_%ED%94%84%EB%A1%9C%ED%8D%BC%ED%8B%B0_%EB%B0%A9%EC%8B%9D

 

[jQuery] ๐Ÿ“š ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ this / ์ œ์ด์ฟผ๋ฆฌ $(this) ์ฐจ์ด

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—๋Š” this๊ฐ€ ์žˆ๊ณ , ์ œ์ด์ฟผ๋ฆฌ์—๋Š” $(this) ๊ฐ€ ์žˆ๋‹ค. โ€‹์ด๋ฒคํŠธ ๊ฐ์ฒด์—์„œ์˜ ์ด ๋‘˜์˜ ์ฐจ์ด์ ๊ณผ ๋”๋ถˆ์–ด ์‚ฌ์šฉ๋ฒ•์„ ์•Œ์•„๋ณด์ž. ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ this โ€‹ ๐Ÿ“Œ ์ธ๋ผ์ธ ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ ๋ฐฉ์‹ ์ธ๋ผ์ธ ์ด๋ฒค

inpa.tistory.com

 

 

<!DOCTYPE html>
<html lang="ko">
<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>
    <ul>
        <li>๋”ธ๊ธฐ</li>
        <li>๋ฐ”๋‚˜๋‚˜</li>
        <li>์ˆ˜๋ฐ•</li>
    </ul>

    <script>
        this.onclick = function(e){
            e.target.style.display="none";
        }
    </script>
</body>
</html>

 

 

<!DOCTYPE html>
<html lang="ko">
<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>
    <ul>
        <li>๋”ธ๊ธฐ</li>
        <li>๋ฐ”๋‚˜๋‚˜</li>
        <li>์ˆ˜๋ฐ•</li>
    </ul>

    <script>
        this.onclick=function(){
            event.target.style.display="none";
        }
    </script>
</body>
</html>

 

<!DOCTYPE html>
<html lang="ko">
<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>
    <ul>
        <li id="a">๋”ธ๊ธฐ</li>
        <li id="b">๋ฐ”๋‚˜๋‚˜</li>
        <li id="c">์‚ฌ๊ณผ</li>
    </ul>

    <script>
        this.onclick=function(e){
            if(e.target.id=="a"){a.style.backgroundColor="red";
            b.style.backgroundColor="white";
            c.style.backgroundColor="white";}
            if(e.target.id=="b"){b.style.backgroundColor="green";
            a.style.backgroundColor="white";
            c.style.backgroundColor="white";}
            if(e.target.id=="c"){c.style.backgroundColor="blue";
            a.style.backgroundColor="white";
            b.style.backgroundColor="white";}
        }
    </script>
</body>
</html>

 

 

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

<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>

    <style>
        #c {
            cursor: pointer;
        }
    </style>

</head>

<body>
    <ul>
        <li id="a">๋”ธ๊ธฐ</li>
        <li id="b">๋ฐ”๋‚˜๋‚˜</li>
        <li id="c">์‚ฌ๊ณผ</li>
    </ul>

    <script>
        this.onmouseover = function (e) {
            if (e.target.id == "a") { a.style.backgroundColor = "red", a.style.cursor = "pointer" }
            if (e.target.id == "b") { b.style.backgroundColor = "green"; }
            if (e.target.id == "c") { c.style.backgroundColor = "blue"; }
        }

        this.onmouseout = function (e) {
            if (e.target.id == "a") { a.style.backgroundColor = "white"; }
            if (e.target.id == "b") { b.style.backgroundColor = "white"; }
            if (e.target.id == "c") { c.style.backgroundColor = "white"; }
        }
    </script>
</body>

</html>

 

 

<!DOCTYPE html>
<html lang="ko">
<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>

    <style>
        h1{
            cursor: pointer;
        }
    </style>
</head>
<body>
    <h1 id="ach">๋ฐฐ๊ฒฝ ์ƒ‰์ƒ ๋ฐ”๊พธ๊ธฐ</h1>

    <script>
        this.onclick=function(e){
            if(e.target.id=="ach"){
                var arrColor=["#ff0", "#6c0", "#fcf", "#cf0", "#39f"];
                var arrNum=Math.floor(Math.random()*arrColor.length);
                var bodyTag = document.getElementsByTagName("body")[0];
                bodyTag.style.backgroundColor=arrColor[arrNum];
            }
        }
    </script>
</body>
</html>

 

 

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

<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>
    <p id="a">์•ˆ๋…•</p>
    <p id="b">ํ˜ธํ˜ธํ˜ธ</p>
</body>

<script>
    this.onclick = function(event){
        if(event.target.id == "a"){a.style.color="red"; b.style.color="black";}
        if(event.target.id == "b"){b.style.color="red"; a.style.color="black";}
    }
</script>

</html>

 

 

<!DOCTYPE html>
<html lang="ko">
<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>
    <p id="demo"></p>

    <script>
        const person = {
            firstName: "John",
            lastName: "Doe",
            id: 5566,
            fullName: function(){
                return this.firstName + " " + this.lastName;
            }
        };

        document.getElementById("demo").innerHTML = person.fullName();
    </script>
</body>
</html>

 

๋Œ“๊ธ€