티스토리 뷰

아래는 예제입니다.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<!DOCTYPE html>
<html lang="ko">
<head>
    <title>Title</title>
    <script src="js/jquery-3.3.1.min.js"></script>
    
    <style>
        #buttonP span{
            font-size: 30px;
            font-weight: bold;
            cursor: pointer;
        } 
        #textP{
            font-size: 30px;
            font-weight: bold;
            color: aqua;
        }
    </style>
    
    <script>
        $(function(){       // jquery 시작
            /*
                show() / hide()  
            */
            $("#showBtn").click(function(){
                $("#textP").text("show() 버튼을 클릭했습니다.")
                $("#textP").show();
            });
            $("#hideBTN").click(function(){
                $("#textP").hide();
            });
            $("#displayBTN").click(function(){
                $("#textP").text("display 버튼을 클릭했습니다.")
                $("#textP").css("display","");
            });
            $("#displayNonBTN").click(function(){
                $("#textP").css("display","none");
            });
        });
    </script>
    
</head>
    <body>
        <p id="buttonP">
            <span id="showBtn">show()</span> / 
            <span id="hideBTN">hide()</span> / 
            <span id="displayBTN">display</span>/ 
            <span id="displayNonBTN">display:none</span>
        </p>
        <p id="textP"></p>
    </body>
</html>
cs


아래는 미리보기입니다.


원하는 버튼을 클릭해주세요!!

show() / hide() / display / display:none



예제를 라인별로 보겠습니다.


43
44
45
46
47
48
49
50
51
    <body>
        <p id="buttonP">
            <span id="showBtn">show()</span> / 
            <span id="hideBTN">hide()</span> / 
            <span id="displayBTN">display</span>/ 
            <span id="displayNonBTN">display:none</span>
        </p>
        <p id="textP"></p>
    </body>
cs

먼저 html 입니다. p태그와 span 태그를 이용해서 버튼 4개를 만들었습니다.

그리고 show() 와 hide() 로 숨겼다 보여졌다할 p태그를 하나더 생성했습니다.


7
8
9
10
11
12
13
14
15
16
17
18
    <style>
        #buttonP span{
            font-size: 30px;
            font-weight: bold;
            cursor: pointer;
        } 
        #textP{
            font-size: 30px;
            font-weight: bold;
            color: aqua;
        }
    </style>
cs

위 html 에 style을 줬습니다.


25
26
27
28
            $("#showBtn").click(function(){
                $("#textP").text("show() 버튼을 클릭했습니다.")
                $("#textP").show();
            });
cs

라인 25.    'showBtn' 이라는 아이디를 가진 span에 클릭이벤트를 설정합니다.

   아래는 클릭이벤트 예제입니다.

   >>[[JQUERY]자바스크립트] - jQuery 020. 이벤트 on(), 클릭이벤트 click()

라인 26.    'textP' 라는 아이디를 가진 p태그의 텍스트 값을 text()를 이용해 설정합니다.

   아래는 text()의 예제 입니다.

   >>[[JQUERY]자바스크립트] - jQuery 027. 텍스트 값 가져오기 text()

라인 27.    show() 함수를 활용해 p태그를 보여줍니다. 


29
30
31
            $("#hideBTN").click(function(){
                $("#textP").hide();
            });
cs

라인 29.    'hideBtn' 이라는 아이디를 가진 span에 클릭이벤트를 설정합니다.

라인 30.    아이디값으로 'textP' 를 가진 p태그를 hied()를 활용해 숨깁니다.


32
33
34
35
            $("#displayBTN").click(function(){
                $("#textP").text("display 버튼을 클릭했습니다.")
                $("#textP").css("display","");
            });
cs

라인 32.    'displayBTN' 이라는 아이디를 가진 span에 클릭이벤트를 설정합니다.  

라인 33.    'textP' 라는 아이디를 가진 p태그의 텍스트 값을 text()를 이용해 설정합니다.

라인 34.    css()를 이용해서 p태그의 display 값을 "" 없애줍니다.


36
37
38
            $("#displayNonBTN").click(function(){
                $("#textP").css("display","none");
            });
cs

라인 36.    'displayNonBTN' 이라는 아이디를 가진 span에 클릭이벤트를 설정합니다. 

라인 37.    css()를 이용해서 p태그의 display 값을 "none" 로 바꿔줍니다.


## show() 는 요소의 display 값이 있는 것과 같고, hide() 는 요소의 display 값이 "none"와 같습니다.      


댓글