🖋️
JQuery
  • 제이쿼리
  • 제이쿼리 기본
  • 선택자
    • 기본 선택자
    • 계층 선택자
    • 속성 선택자
    • 기본 필터 선택자
    • 내용 필터 선택자
    • 보임 필터 선택자
    • 자식 요소 필터 선택자
    • 폼 요소 필터 선택자
  • 탐색
    • .find()/filter()
    • each()/$.each()
  • 속성
    • addClass() / removeClass()
    • toggleClass()
    • hasClass()
    • attr()/removeAttr()
    • position()/offset()
    • scrollTop()/scrollLeft()
  • 변경
    • text()/html()
    • append()/prepend()
    • remove() / empty()
  • 애니메이션
    • show()/hide()
    • fadeIn() /fadeOut()
    • slideUp()/slideDown()
    • animate()
  • 이벤트
Powered by GitBook
On this page

Was this helpful?

  1. 변경

text()/html()

html() 메서드는 선택한 요소의 하위 요소를 문자열로 반환하거나 하위 요소를 전부 제거하고 새 요소로 바꿀 때 사용합니다. text() 메서드는 선택한 요소에 포함되어 있는 전체 텍스트를 가져오거나 선택한 하위 요소를 전부 제거하고 새 텍스트를 생성할 때 사용합니다.

$("선택자").text(); $("선택자").text("변경할 요소"); $("선택자").html(); $("선택자").html("변경할 요소");

<!DOCTYPE html>
<html lang="ko">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>JQuery</title>
    <style>
         @font-face {
            font-family: 'MapoFlowerIsland';
            src: url('https://cdn.jsdelivr.net/gh/projectnoonnu/noonfonts_2001@1.1/MapoFlowerIslandA.woff') format('woff');
            font-weight: normal;
            font-style: normal;
        }
        body {font-family: 'MapoFlowerIsland';}
        li.red {
            color: #c7254e;
            background-color: #f9f2f4;
            border: 1px dashed #a51a3d;
        }
    </style>
</head>
<body>
    
    <h1>탐색(Traversing)</h1>
    <ul>
        <li>addClass()메서드는 선택한 요소에 클래스를 생성하고, removeClass() 메서드는 선택한 요소에서 지정한 클래스를 삭제합니다.</li>
        <li>toggleClass() 메서드는 선택한 요소에 지정한 클래스가 없으면 생성하고 있을 경우에는 삭제합니다.</li>
        <li>hasClass() 메서드는 선택한 요소에 지정한 클래스가 있으면 true를 반환하고 없으면 false를 반환합니다.</li>
    </ul>
    <button class="btn1">btn1</button>
    <button class="btn2">btn2</button>
    <button class="btn3">btn3</button>
    <button class="btn4">btn4</button>

    <!-- script -->
    <script src="jquery.min_1.12.4.js"></script>
    <script>
        //버튼을 클릭하면 첫 번째 li의 글씨를 경고창으로 표시
        $(".btn1").click(function(){
           let text = $("li:eq(0)").text();
           alert(text);
        });

        // 버튼2를 클릭하면 마지막 li의 글씨를 변경
        //  text() 메서드는 선택한 요소에 포함되어 있는 전체 텍스트를 가져오거나 선택한 하위 요소를 전부 제거하고 새 텍스트를 생성할 때 사용합니다.
        $(".btn2").click(function(){
           $("li:eq(2)").text("text() 메서드는 선택한 요소에 포함되어 있는 전체 텍스트를 가져오거나 선택한 하위 요소를 전부 제거하고 새 텍스트를 생성할 때 사용합니다.");
        });

        $(".btn3").click(function(){
            $("li:eq(1)").html("<strong>addClass()</strong>메서드는 선택한 요소에 클래스를 생성하고, removeClass() 메서드는 선택한 요소에서 지정한 클래스를 삭제합니다.");
        });

        $(".btn4").click(function(){
            $("li:eq(1)").text("<strong>addClass()</strong>메서드는 선택한 요소에 클래스를 생성하고, removeClass() 메서드는 선택한 요소에서 지정한 클래스를 삭제합니다.");
        }); 
    </script>
</body>
</html>
// h1태그를 클릭하면 모든 글씨 빨갛게
$("h1").click(function(){
    $("*").css("color","red");
});

// h1태그를 클릭하면 클릭한 글씨를 alert으로 출력
$("h1").click(function(){
    const txt = $(this).text();
    alert(txt);
});
Previous변경Nextappend()/prepend()

Last updated 4 years ago

Was this helpful?