yongyong-e

[jQuery] 문서 객체 생성 방법 본문

웹 프로그래밍/jQuery

[jQuery] 문서 객체 생성 방법

Yonghan Kim 2019. 9. 4. 09:31

1. CSS 선택자 사용

<!DOCTYPE html>
<html>
<head>
    <title>jQuery 객체 생성</title>
    <script src="https://code.jquery.com/jquery-3.4.1.js"></script>
</head>
<body>
    <h1></h1>
    <script>
        $("h1").css("color", "blue")
        $("h1").html("Hello World")
    </script>
</body>
</html>

 

2. 태그 형태의 문자열 사용

<!DOCTYPE html>
<html>
<head>
    <title>jQuery 객체 생성</title>
    <script src="https://code.jquery.com/jquery-3.4.1.js"></script>
</head>
<body>
    <h1></h1>
    <script>
        $("<h1></h1>")
            .appendTo("body")
            .css("color", "green")
            .html("Hello World")
    </script>
</body>
</html>

 

3. 문서 객체 사용

<!DOCTYPE html>
<html>
<head>
    <title>jQuery 객체 생성</title>
    <script src="https://code.jquery.com/jquery-3.4.1.js"></script>
</head>
<body>
    <h1></h1>
    <script>
        var header = document.querySelector("h1")
        $(header).css("color", "blue")
        $(header).html("Hello World")
    </script>
</body>
</html>

 

Comments