WEB/HTML+CSS+JS

[JavaScript] document.write()ํ•จ์ˆ˜ ์œ„์น˜

kite707 2021. 4. 9.

JS๋กœ ์ด๊ฒƒ์ €๊ฒƒ ํ•ด๋ณด๋‹ค๊ฐ€ document.write()ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉ์‹œ ์–ธ์ œ๋Š” ์ƒˆ๋กœ์šด ํŽ˜์ด์ง€์— ์ถœ๋ ฅ๋˜๊ณ , ์–ธ์ œ๋Š” ํ˜„์žฌ ํŽ˜์ด์ง€์— ์ž‘์„ฑ๋˜๊ธธ๋ž˜ ๊ถ๊ธˆํ•ด์„œ ์•Œ์•„๋ดค๋‹ค.

 

document.write()์™€ document.writeln()

์šฐ์„  document.write()ํ•จ์ˆ˜์— ๋Œ€ํ•ด ์•Œ์•„๋ณด์ž.  ๋ง ๊ทธ๋Œ€๋กœ ๊ด„ํ˜ธ ์•ˆ์— ๋“ค์–ด๊ฐ€์žˆ๋Š” ๊ฒƒ์„ ํŽ˜์ด์ง€์— "์“ฐ๋Š”(์ถœ๋ ฅํ•˜๋Š”)" ํ•จ์ˆ˜์ด๋‹ค. ์•„๋ž˜ ์˜ˆ์ œ๋ฅผ ๋ณด๋ฉด ์ดํ•ดํ•  ์ˆ˜ ์žˆ๋‹ค.

<!DOCTYPE html>
<html lang="en">
<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>
    <script>
        document.write("์ด๋ ‡๊ฒŒ ํŽ˜์ด์ง€์— ๊ธ€์ž๋ฅผ ํ‘œ์‹œํ•ฉ๋‹ˆ๋‹ค.");
    </script>
</body>
</html>

์•„๋ž˜์™€ ๊ฐ™์ด ๊ธ€์ž๋ฅผ ์จ์ฃผ๋Š” ํ•จ์ˆ˜์ด๋‹ค.

document.write()ํ•˜๋ฉด document.writeln()ํ•จ์ˆ˜๋„ ์žˆ๋‹ค. writeln()ํ•จ์ˆ˜๋Š” ์ž…๋ ฅํ•œ ๊ฐ’์— "\n"์„ ์ถ”๊ฐ€ํ•˜์—ฌ ์ž…๋ ฅํ•ด์ค€๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์‹ค์ œ๋กœ ์‚ฌ์šฉํ•ด๋ณด๋ฉด ์ค„๋ฐ”๊ฟˆ์ด ์•„๋‹ˆ๋ผ ํ•œ์นธ์”ฉ ๋„์–ด์“ฐ๊ธฐ๋˜์–ด ์ถœ๋ ฅ๋˜๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

<!DOCTYPE html>
<html lang="en">
<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>
    
    <script>
        document.writeln("writelnํ•จ์ˆ˜");
        document.writeln("writelnํ•จ์ˆ˜");
        document.writeln("writelnํ•จ์ˆ˜<br>");
        document.write("writeํ•จ์ˆ˜");
        document.write("writeํ•จ์ˆ˜");
        document.write("writeํ•จ์ˆ˜");
        document.write("writeํ•จ์ˆ˜");
    </script>
</body>
</html>

 

์œ„ ์ฝ”๋“œ๋ฅผ ๋ณด๋ฉด <br>๋ฅผ ์‚ฌ์šฉํ•ด์•ผ๋งŒ ์ค„๋ฐ”๊ฟˆ์ด ๋œ๋‹ค๋Š” ์ , writeln์€ ํ•œ์นธ ๋„์–ด์“ฐ๊ธฐ๊ฐ€ ๋˜๊ณ , write๋Š” ๊ทธ๋ƒฅ ๋ถ™์–ด์„œ ์ถœ๋ ฅ๋˜๋Š”์ ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค. html์—์„œ๋Š” ์ค„๋ฐ”๊ฟˆ์„ ํ•˜๋ ค๋ฉด \n์ด ์•„๋‹ˆ๋ผ <br>์„ ์‚ฌ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ค„๋ฐ”๊ฟˆ์ด ์•ˆ๋˜๋Š” ๊ฒƒ์ด๋‹ค. ๋‹ค๋งŒ <pre>ํƒœ๊ทธ ์•ˆ์—์„œ๋Š” \n์ด ๋จนํžˆ๊ธฐ ๋•Œ๋ฌธ์— <pre>ํƒœ๊ทธ ์•ˆ์—์„œ document.writeln()์„ ์‚ฌ์šฉํ•˜๋ฉด ์˜๋„ํ•œ๋Œ€๋กœ ์ค„๋ฐ”๊ฟˆ์ด ์ž˜ ๋œ๋‹ค.

 

document.write()์˜ ์ž…๋ ฅ์œ„์น˜

์•ž์—์„œ document.write()ํ•จ์ˆ˜๊ฐ€ ๋ฌด์—‡์„ ํ•˜๋Š”๊ฑด์ง€ ์•Œ์•„๋ณด์•˜๋‹ค. ๊ทธ๋Ÿผ ์ฒ˜์Œ์˜ ๊ถ๊ธˆ์ฆ์œผ๋กœ ๋Œ์•„๊ฐ€๋ณด์ž. ์–ด๋–ป๊ฒŒ ์‚ฌ์šฉํ•ด์•ผ ์ƒˆ ์ฐฝ์— ์ถœ๋ ฅํ•˜๊ณ  ์–ด๋–ป๊ฒŒ ์‚ฌ์šฉํ•ด์•ผ ํ˜„์žฌ ์ฐฝ์— ๊ฐ’์„ ์ž…๋ ฅํ•˜๋Š”๊ฑธ๊นŒ? ๊ตฌ๊ธ€๋ง์„ ํ†ตํ•ด <script></script>ํƒœ๊ทธ ์•ˆ์—์„œ document.write()๋ฅผ ์ž…๋ ฅํ•˜๋ฉด ํ˜„์žฌ ํŽ˜์ด์ง€์—, ๋‹ค๋ฅธ ๊ฒฝ์šฐ์—๋Š” ์ƒˆ๋กœ์šด ์ฐฝ์— ์ž…๋ ฅ๋œ๋‹ค๊ณ  ํ•œ๋‹ค.

 

์‹คํ—˜์„ ์œ„ํ•ด ์ฝ”๋“œ๋ฅผ ์•„๋ž˜์™€ ๊ฐ™์ด ๊ตฌ์„ฑํ–ˆ๋‹ค.

<!DOCTYPE html>
<html lang="en">
<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>
    <script>
        function ss(){
            document.write("<h1>์ด๊ฒƒ์€ writeํ•จ์ˆ˜ ์‹ค์Šต์ž…๋‹ˆ๋‹ค.</h1>");
        }
        function ss2(){
            document.writeln("<h1>์ด๊ฒƒ์€ writelnํ•จ์ˆ˜ ์‹ค์Šต์ž…๋‹ˆ๋‹ค.</h1>");
        }
    </script>
</head>
<body>
    <a href="javascript:ss()">writeํ•จ์ˆ˜ ์‹ค์Šต</a>
    <a href="javascript:ss2()">writelnํ•จ์ˆ˜ ์‹ค์Šต</a>
    <script>
        document.write("์Šคํฌ๋ฆฝํŠธ ํƒœ๊ทธ ๋‚ด์—์„œ ์ž‘์„ฑ");
    </script>
</body>
</html>

์šฐ์„  ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ๋ถˆ๋Ÿฌ์˜ค๋ฉด ์ƒˆ๋กœ์šด ์ฐฝ์— ๊ธ€์ด ์จ์ง„๋‹ค.

๊ทธ๋ฆฌ๊ณ  ๊ทธ๋ƒฅ <script>ํƒœ๊ทธ์— ์ž‘์„ฑํ•œ ๊ฒฝ์šฐ์—๋Š”

์ด๋ ‡๊ฒŒ ์›๋ž˜ ํŽ˜์ด์ง€์— ์จ์ง„๋‹ค.

 

innerHTML ์‚ฌ์šฉ๋ฒ•

ํ•ด๊ฒฐ์ฑ…์œผ๋กœ๋Š” ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ์—๋Š” innerHTML์„ ์“ฐ๋Š” ๊ฒƒ์ด๋‹ค. ์‚ฌ์šฉ๋ฒ•์€ ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

getElementById์˜ ๊ฒฝ์šฐ๋Š” ๋‹ค๋ฅธ๊ฒƒ์œผ๋กœ ๋Œ€์ฒดํ•  ์ˆ˜ ์žˆ๋‹ค.(bodyํƒœ๊ทธ์— ์“ฐ๊ณ ์‹ถ์œผ๋ฉด body ์ด๋Ÿฐ์‹์œผ๋กœ)

 document.getElementById("inner").innerHTML="์“ธ ๋‚ด์šฉ";

innerHTML์„ ํ†ตํ•ด ssํ•จ์ˆ˜๋ฅผ ์ˆ˜์ •ํ•˜์˜€๋‹ค. ์ฝ”๋“œ๋Š” ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

<!DOCTYPE html>
<html lang="en">
<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>
    <script>
        function ss(){
        //๋ฐ”๋€๋ถ€๋ถ„!
            document.getElementById("inner").innerHTML="์ด๊ฒƒ์€ writeํ•จ์ˆ˜ ์‹ค์Šต์ž…๋‹ˆ๋‹ค";
        }
        function ss2(){
            document.writeln("<h1>์ด๊ฒƒ์€ writelnํ•จ์ˆ˜ ์‹ค์Šต์ž…๋‹ˆ๋‹ค.</h1>")
        }
    </script>
</head>
<body>
    <a href="javascript:ss()">writeํ•จ์ˆ˜ ์‹ค์Šต</a>
    <a href="javascript:ss2()">writelnํ•จ์ˆ˜ ์‹ค์Šต</a>
    <div id="inner"></div>
    <script>
        document.write("์Šคํฌ๋ฆฝํŠธ ํƒœ๊ทธ ๋‚ด์—์„œ ์ž‘์„ฑ");
    </script>
</body>
</html>

 

์œ„ ์ฝ”๋“œ์—์„œ ์˜๋ฏธ์—†๋Š” ํƒœ๊ทธ div๋ฅผ ๋งŒ๋“ค์—ˆ๋Š”๋ฐ <p>๋ฅผ ์‚ฌ์šฉํ•˜๊ฑฐ๋‚˜ <span>์„ ์‚ฌ์šฉํ•ด๋„ ๊ด€๊ณ„์—†๋‹ค. ์œ„ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•ด๋ณด๋ฉด ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

write ํ•จ์ˆ˜ ์‹ค์Šต์„ ๋ˆ„๋ฅธ ๋ชจ์Šต(innerHTML)

 

writelnํ•จ์ˆ˜ ์‹ค์Šต์„ ๋ˆ„๋ฅธ ๋ชจ์Šต(write)

innerHTML๋กœ write๋ฅผ ๋Œ€์ฒดํ•  ์ˆ˜ ์žˆ์œผ๋‹ˆ innerHTML์„ ์“ฐ๋Š”๊ฒŒ ๋” ์ข‹์•„๋ณด์ธ๋‹ค.

 

 

๋Œ“๊ธ€