Day031

RISK_TAKERยท2023๋…„ 3์›” 15์ผ

๐Ÿ’ก Math ๊ฐ์ฒด

  • ๊ด€๋ จ ๋ฉ”์†Œ๋“œ
    random(): 0~1์‚ฌ์ด์˜ ์ˆ˜๋ฅผ ๋ฝ‘๋Š”๋‹ค.
    floor(): ์†Œ์ˆ˜์  ๋ฒ„๋ฆผ
    round(): ์†Œ์ˆ˜์  ๋ฐ˜์˜ฌ๋ฆผ
    ceil(): ์†Œ์ˆ˜์  ์˜ฌ๋ฆผ
  • From ~ To ์‚ฌ์ด์˜ ๋žœ๋คํ•œ ์ˆซ์ž๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ๋ฐฉ๋ฒ•
	Math.floor(Math.random() * (To - From + 1) ) + From;

๐Ÿ’ก Date ๊ฐ์ฒด

  • ์‹œ๊ฐ„์„ ์ถœ๋ ฅํ•˜๋Š” ๋ฐฉ๋ฒ•
  • js์—์„œ ์›”์— ํ•ด๋‹นํ•˜๋Š” ๊ฐ’์€ 0~11์ด๋‹ค.
  • ์‹œ๊ฐ„์€ UTC ์‹œ๊ฐ„์ด ๊ธฐ์ค€
  • UTC + 9 = ํ•œ๊ตญ์‹œ๊ฐ„
	let date1 = new Date();
	console.log(date1); //ํ˜„์žฌ ์‹œ๊ฐ„์„ ์ถœ๋ ฅ
	
	let date2 = new Date(2023, 3, 15); //๋…„์›”์ผ
			   //์ถœ๋ ฅ๊ฐ’ : 2023-04-14T15:00:00.000Z
	let date2_2 = new Date("2023-03-15"); //๋…„์›”์ผ
			   //์ถœ๋ ฅ๊ฐ’ : 2023-03-15T00:00:00.000Z
	let diff9Hours = 1000 * 60 * 60 * 9; //9์‹œ๊ฐ„
  • ๊ด€๋ จ ๋ฉ”์†Œ๋“œ
    getFullYear()
    getMonth()
    getDate()
    getHours
    getMinutes()
    getSeconds()
    getTime() : ๋ฐ€๋ฆฌ ์ดˆ๋‹จ์œ„์˜ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

๐Ÿ’ก ๋ธŒ๋ผ์šฐ์ € ๊ฐ์ฒด ๋ชจ๋ธ

  • ์›น๋ธŒ๋ผ์šฐ์ €์—์„œ ์ œ๊ณตํ•˜๋Š” ๊ฐ์ฒด ๋ชจ๋ธ

  • window ๊ฐ์ฒด์˜ ์†์„ฑ

  • ๊ด€๋ จ ๋ฉ”์†Œ๋“œ
    open(๊ฒฝ๋กœ, ์ด๋ฆ„, ์†์„ฑ) : ๋ธŒ๋ผ์šฐ์ €์—์„œ ์ƒˆ์ฐฝ์„ ์—ฐ๋‹ค.
    scrollTo() : ์›น๋ธŒ๋ผ์šฐ์ €์˜ ์Šคํฌ๋กค ์œ„์น˜๋ฅผ ํŠน์ • ์œ„์น˜๋กœ ์ด๋™ํ•œ๋‹ค.
    scrollBy() : ์Šคํฌ๋กค์˜ ์œ„์น˜๋ฅผ ํ˜„์žฌ ์œ„์น˜์—์„œ ์ƒ๋Œ€ ์œ„์น˜๋กœ ์ด๋™ํ•œ๋‹ค.

<body>
    <button onclick="openNew()" style="top:200px">์ฐฝ๋„์šฐ๊ธฐ</button>
    <button onclick="closeNew()" style="top:250px">์ฐฝ๋‹ซ๊ธฐ</button>
    <button onclick="windowInfo()">์†์„ฑํ™•์ธ</button>
    <button onclick="sclTo()" style="top:100px;">์Šคํฌ๋กคTo</button>
    <button onclick="sclBy()" style="top:150px;">์Šคํฌ๋กคBy</button>
    <script>
        function openNew() {
            window.open('https://www.naver.com', 'title', 'width=200, height=200, left=1600, top=200');
        }
        // alert('์•Œ๋ฆผ์ฐฝ ํ…์ŠคํŠธ');
        function closeNew() {
            window.close();
        }
        function windowInfo() {
            console.log(window.innerWidth);
            console.log(window.innerHeight);
            console.log(window.outerWidth);
            console.log(window.outerHeight);
            console.log(window.screenTop);
            console.log(window.screenLeft);
            console.log(window.screenX);
            console.log(window.screenY);
            console.log(window.scrollX);
            console.log(window.scrollY);
        }
        function sclTo(){
            // window.scrollTo(200,200);
            window.scrollTo({left:200, top:200, behavior:'smooth'});
        }
        function sclBy(){
            // window.scrollTo(200,200);
            window.scrollTo({left:200, top:200, behavior:'smooth'});
            // window.scrollBy(200, 200);
            window.scrollBy({left:200, top:200, behavior:'smooth'});
        }
    </script>
</body>

  • V8์—”์ง„ : js๋ฅผ ์‹คํ–‰์‹œํ‚ฌ ์ˆ˜ ์žˆ๋„๋ก ๋งŒ๋“ค์–ด์ฃผ๋Š” ์—”์ง„, Node.js๋ฅผ ์„ค์น˜ํ•˜๋ฉด์„œ ์„ค์น˜๊ฐ€ ๋˜์—ˆ๋‹ค.
    ์ž‘์„ฑํ•œ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜๋ฉด V8์—”์ง„์„ ํ†ตํ•ด์„œ console๋กœ ๊ฒฐ๊ณผ๋ฅผ ๋ณด์—ฌ์ค€๋‹ค.

  • ๋ธŒ๋ผ์šฐ์ €๊ฐ์ฒด๋ชจ๋ธ : ๋ธŒ๋ผ์šฐ์ €์™€ ์†Œํ†ตํ•˜๋Š” ์ฝ”๋“œ, ์ถœ๋ ฅ๋˜๋Š” ๊ฒฐ๊ณผ๋ฅผ ๋ณด๊ธฐ์œ„ํ•ด์„œ๋Š” htmlํŒŒ์ผ ๋‚ด์— script ํƒœ๊ทธ ์•ˆ์— ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•œ ํ›„ ๋ธŒ๋ผ์šฐ์ €๋ฅผ ํ†ตํ•ด ์‹คํ–‰ํ•˜์—ฌ ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•œ๋‹ค.

๐Ÿ’ก ๋ฌธ์„œ ๊ฐ์ฒด ๋ชจ๋ธ(DOM, Document Object Model)

  • ์›น ๋ธŒ๋ผ์šฐ์ €์— ํ‘œ์‹œ๋˜๋Š” ๋ฌธ์„œ๋ฅผ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๊ฐ€ ์ดํ•ดํ•  ์ˆ˜ ์žˆ๋„๋ก ๊ฐ์ฒดํ™”ํ•œ ๋ชจ๋ธ ๊ตฌ์กฐ

  • ๋ฉ”์†Œ๋“œ๋กœ ๋…ธ๋“œ ์„ ํƒํ•˜๊ธฐ
    getElementById()
    getElementsByClassName()
    getElementsByTagName()
    querySelector()
    querySelectorAll()

<p>pํƒœ๊ทธ1</p>
    <p>pํƒœ๊ทธ2</p>
    <div id="div1">๊ณ ์œ ์•„์ด๋””</div>
    <div class="divcls">ํด๋ž˜์Šค1</div>
    <div class="divcls">ํด๋ž˜์Šค2</div>

    <div class="conatainer1" id="targetid">
        <div class="indiv"></div>
        <div class="indiv"></div>
        <div class="indiv"></div>
    </div>
    <div class="container2">
        <div class="indiv"></div>
    </div>
    <script>
        const el1 = document.getElementById("div1");
        const el2s = document.getElementsByClassName("divcls");
        const el3s = document.getElementsByTagName("p");
        const el2 = document.getElementById("targetid");
        console.log(el1);
        console.log(el2s);
        console.log(el3s);

        const qs1 = document.querySelector("#div1");
        const qs2s = document.querySelectorAll(".divcls");
        const qs3s = document.querySelectorAll(".indiv");
        const qs4s = document.querySelectorAll(".container1 .indiv");
		const qs5s = document.querySelectorAll("p");
        console.log(qs1);
        console.log(qs2s);
        console.log(qs3s);
        console.log(qs4s);
    </script>
  • ์ฝ˜ํ…์ธ  ์กฐ์ž‘ํ•˜๊ธฐ
    textContent : ๋…ธ๋“œ ์š”์†Œ์˜ ๋ชจ๋“  ํ…์ŠคํŠธ์— ์ ‘๊ทผ
    innerContent : ๋…ธ๋“œ ์š”์†Œ์˜ ํ…์ŠคํŠธ ์ค‘์›น๋ธŒ๋ผ์šฐ์ €์— ๋ณด์ด๋Š” ํ…์ŠคํŠธ์—๋งŒ ์ ‘๊ทผ
    innerHTML : ๋…ธ๋“œ ์š”์†Œ์˜ ํ…์ŠคํŠธ ์ค‘ HTMLํƒœ๊ทธ๋ฅผ ํฌํ•จํ•œ ํ…์ŠคํŠธ์—๋งŒ ์ ‘๊ทผ
		<p id="title">pTag๋ฌธ์ž, <span style="display: none;">Span ํƒœ๊ทธ</span></p>

    	<script>
        	console.log(document.getElementById('title').textContent);
        	console.log(document.getElementById('title').innerText);
        	console.log(document.getElementById('title').innerHTML);
		</script>
  • ํ…์ŠคํŠธ ์ถ”๊ฐ€ํ•˜๊ธฐ, ์Šคํƒ€์ผ ์ ์šฉํ•˜๊ธฐ
	<p id="t1"></P>
    <P id="t2"></P>
    <P id="t3"></P>

    <script>
        document.querySelector('#t1').textContent = '<strong>textContent</strong> ์†์„ฑ';
        document.querySelector('#t2').innerText = '<strong>textContent</strong> ์†์„ฑ';
        document.querySelector('#t3').innerHTML = '<strong>textContent</strong> ์†์„ฑ';

 		document.querySelector('#t1').style.color = 'red';
        const pt2 = document.querySelector('#t2');
        pt2.style.color = 'blue';
        const pt3 = document.getElementById('title');
        pt3.style.fontSize = '30px'
	</script>

  • ํด๋ž˜์Šค ์†์„ฑ ์กฐ์ž‘ํ•˜๊ธฐ
<style>
	.box {
		width: 100px;
        height: 100px;
        border: 1px solid black;
	}
    .redbox {
    	background-color: red;
	}
    .bluebox {
    	background-color: blue;
	}
    .greenbox {
    	background-color: green;
	}
</style>

<body>

    <div id="box1" class="box"></div>
    <div id="box2" class="box"></div>
    <div id="box3" class="box"></div>
    <div id="box4" class="box redbox"></div>

    <script>
        document.getElementById('box1').classList.add('redbox');
        document.querySelector('#box2').classList.add('bluebox');
        document.querySelector('#box3').classList.add('greenbox');       
        document.getElementById('box4').classList.remove('redbox');
	</script>
</body>
  • ํ† ๊ธ€(toggle) ์ ์šฉํ•˜๊ธฐ
<head>
	<style>
        .box {
            width: 100px;
            height: 100px;
            border: 1px solid black;
        }
        .redbox {
            background-color: red;
        }        
        button {
            color: white;
            background-color: black;
        }
    </style>
</head>
<body>
	<div id="box1" class="box"></div>
    <div id="box2" class="box"></div>
    <div id="box3" class="box"></div>
    <div id="box4" class="box redbox"></div>
    <button onclick="box4toggle()">๋„ค๋ฒˆ์งธ ๋ฐ•์Šค ํ† ๊ธ€</button>

    <script>
        function box4toggle() {
              document.getElementById('box4').classList.toggle('redbox');
        }
    </script>
</body>
  • ๋ฉ”์†Œ๋“œ๋กœ ์†์„ฑ ์กฐ์ž‘ํ•˜๊ธฐ
	<a href="https://www.naver.com" target="_blank">๋„ค์ด๋ฒ„๋กœ ์ด๋™</a>
    <script>       
        const atag = document.querySelector('a');
        console.log(atag.getAttribute("href"));
        console.log(atag.getAttribute("target"));
        atag.removeAttribute("href");

        atag.setAttribute("href", "https://www.daum.net")
    </script>
  • ์†์„ฑ ์ถ”๊ฐ€, ์†์„ฑ๊ฐ’ ์„ค์ •
	document.getElementById('box1').classList.add('redbox'); //ํด๋ž˜์Šค ์ถ”๊ฐ€ํ•˜๋Š” ๊ฒƒ
	document.getElementById('box2').setAttribute("class", "box bluebox"); //ํด๋ž˜์Šค ๊ฐ’ ์ž์ฒด๋ฅผ ๋ณ€๊ฒฝํ•˜๋Š” ๊ฒƒ
  • ๋…ธ๋“œ ์ถ”๊ฐ€/์‚ญ์ œํ•˜๊ธฐ
<body>

    <div id="container">
        <p id="p1">p1</p>
        <p id="p2">p2</p>
    </div>

    <script>
        const newEl = document.createElement("a");

        const linkText = document.createTextNode("๋งํฌ๊ธ€์ž");

        const linkHref = document.createAttribute("href");
        const linkTarget = document.createAttribute("target");
        linkHref.value = "https://www.naver.com";
        linkTarget.value = "_blank";
        newEl.appendChild(linkText);
        newEl.setAttributeNode(linkHref);
        newEl.setAttributeNode(linkTarget);

        document.body.appendChild(newEl);

        const p2 = document.querySelector("#p2");
        p2.parentNode.removeChild(p2);
    </script>
</body>
  • ๋ชจ๋“  ์š”์†Œ๋ฅผ ์ฐพ์•„ ์‚ญ์ œํ•˜๊ธฐ
<body>
    <div id="container">
        <p id="p1">p1</p>
        <p id="p2">p2</p>
    </div>
	<script>
		const p12 = document.querySelectorAll("#container p");

        for(let i=0; i<p12.length; i++){
            p12[i].parentNode.removeChild(p12[i]);
        }
    </script>
</body>

0๊ฐœ์˜ ๋Œ“๊ธ€