jQuery 수업

jQuery-11(get적용- text(), html())

줄라이퍼스트 2020. 2. 19. 14:30

※ text(), html() 예제1

<!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.4.1/jquery.min.js"></script>
    <script>
        $(document).ready(function(){
            
            $("#btn1").click(function(){
                // text() : 객체 내부 text를 가져옴
                alert("Text : " + $("test").text());
            });

            $("#btn2").click(function(){
                // html() : 객체의 내부 text정보와 tag정보까지 같이 가져옴
                //          tag정보까지 같이 가져옴
                alert("HTML : " + $("#test").html());
            });

        });

    </script>
</head>
<body>
    
    <p id="test">This is some <b>bold</b> text in a paragraph.</p>

    <button id="btn1">Show Text</button>
    <button id="btn2">Show HTML</button>

</body>
</html>

※ 실행화면1

 

실행화면1 - 클릭하시면 자세히 보실 수 있습니다.

※ text(), html() 예제2 ( button 추가)

<!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.4.1/jquery.min.js"></script>
    <script>
        $(document).ready(function(){
            
            $("#btn1").click(function(){
                // text() : 객체 내부 text를 가져옴
                alert("Text : " + $("test").text());
            });

            $("#btn2").click(function(){
                // html() : 객체의 내부 text정보와 tag정보까지 같이 가져옴
                //          tag정보까지 같이 가져옴
                alert("HTML : " + $("#test").html());
            });
            $("#btn3").click(function(){
                // input 박스에 있는 value 값을 가져올 떄 사용
                alert("HTML: "+ $("input").val());
            });
            $("#btn4").click(function(){
                // p 태그 안에 있는 a태그 value 값을 가져옴
               alert($("#naver").attr("href"));
            });

        });

    </script>
</head>
<body>
    
    <p id="test">This is some <b>bold</b> text in a paragraph.</p>
    <p>Name: <input type="text" id="test" value="Mickey Mouse"></p>
    <p><a href="https://www.naver.com" id="naver">naver.com</a></p>

    <button id="btn1">Show Text</button>
    <button id="btn2">Show HTML</button>
    <button id="btn3">Show Val</button>
    <button id="btn4">Show href Value</button>


</body>
</html>

※ 실행화면2

 

실행화면2 - 클릭하시면 자세히 보실 수 있습니다.

 

'jQuery 수업' 카테고리의 다른 글

jQuery-13(set적용-attr())  (0) 2020.02.19
jQuery-12(set적용- text(), html())  (0) 2020.02.19
jQuery-10(css- color 적용)  (0) 2020.02.19
jQuery-9(animate()- slideDown())  (0) 2020.02.19
jQuery-8(animate())  (0) 2020.02.19