๋ชฉ์ฐจ
Create Read Update Delete
HTML ์ฐพ๊ธฐ
- doucument.getElementById(id๋ช ) : id๋ก ์ฐพ๊ธฐ
- doucument.getElementsByTagName(ํ๊ทธ๋ช ) : ํ๊ทธ๋ก ์ฐพ๊ธฐ(๋ฐฐ์ด๋ก ๋ฐํ)
- doucument.getElementsByClassName(ํด๋์ค๋ช ) : ํด๋์ค๋ก ์ฐพ๊ธฐ(๋ฐฐ์ด๋ก ๋ฐํ)
- document.querySelector(css ์ ํ์) : css ์ ํ์๋ก ์ฐพ๊ธฐ(์ต์ด๋ฑ์ฅ ํ๋๋ง ๋ฐํ)
- document.querySelectorAll(css ์ ํ์) : css ์ ํ์๋ก ์ฐพ๊ธฐ(๋ชจ๋ ์์. ๋ฐฐ์ด ํํ์ ๊ฐ์ฒด(์ ์ฌ๋ฐฐ์ด)๋ก ๋ฐํ)
(์ธ์๋ก css ์ ํ์๋ฅผ ์ ๋ฌํ๋ค. ' . '(class), ' # '(id), ' * '(all), (tag๋ prefix๋ถ์ด์ง ์์) ๋ฑ์ ์กฐํฉ)
์์
id๋ช ์ผ๋ก ๊ฐ์ ธ์ค๊ธฐ
console.log(document.getElementById('root'))
>>>
<div id='root'>...</div>
ํด๋์ค๋ช ์ผ๋ก ๊ฐ์ ธ์ค๊ธฐ (๋ฐฐ์ด)
console.log(document.getElementsByClassName('el')) //Element's'์ด๋ฉด ๋ฐฐ์ด๋ก ๋ฐํ
>>>
HTMLCollection(17) [div.el, div.el, div.el,
div.el, div.el, div.el, div.el, div.el, div.el,
div.el, div.el, div.el.e_el__dashboard, div.el.e_el__courses,
div.el.e_el__home, div.el.e_el__roadmaps, div.el.e_el__more,
span.el.cart_modal_btn.e_cart_modal_btn.nav-modal-btn]
// 17๊ฐ item ๊ฐ์ง ๋ฐฐ์ด์ด๋ผ๋ ๋ป
CSS ์ ํ์๋ก ๊ฐ์ ธ์ค๊ธฐ
console.log(document.querySelectorAll('a.nav'))
>>> //NodeList ๋ผ๋ ๊ฐ์ฒด ๋ฐํ
NodeList(16) [a.nav, a.nav, a.nav, a.nav,
a.nav.shop, a.nav.shoplive, a.nav, a.nav,
a.nav, a.nav, a.nav, a.nav, a.nav, a.nav, a.nav, a.nav]
HTML ์์ ํ์ธ & ์์
์ ํ์๋ฅผ ์ด์ฉํด ๊ฐ์ ธ์จ ์์ ์ ๊ทผ
- ์์.innerText : ์์ ํ์ธ (๋ด์ฉ๋ง)
- ์์.innerHTML : ํ๊ทธ๊น์ง ํฌํจ
- ์์.attribute : ์์ ์์ฑ ๊ฐ
- ์์.style.property : ์์์ CSS ํ๋กํผํฐ ๊ฐ
์์
innerText
๋ด์ฉ์ ๋ฐํํ๋ค.
element = document.querySelectorAll('li.nav_item')
for (let i=0; i<element.length; i++){
console.log(element[i].innerText)
}
>>>
๋ฉ์ผ
์นดํ
๋ธ๋ก๊ทธ
์ง์iN
์ผํ
์ผํLIVE
Pay
TV
์ฌ์
๋ด์ค
์ฆ๊ถ
๋ถ๋์ฐ
์ง๋
VIBE
์ฑ
์นํฐ
innerHTML
ํ๊ทธ๋ฅผ ๋ฐํํ๋ค.
element = document.querySelectorAll('li.nav_item')
for (let i=0; i<element.length; i++){
console.log(element[i].innerHTML)
}
>>>
<a href="https://mail.naver.com/" class="nav" data-clk="svc.mail"><i class="ico_mail"></i>๋ฉ์ผ</a>
<a href="https://section.cafe.naver.com/" class="nav" data-clk="svc.cafe">์นดํ</a>
<a href="https://section.blog.naver.com/" class="nav" data-clk="svc.blog">๋ธ๋ก๊ทธ</a>
<a href="https://kin.naver.com/" class="nav" data-clk="svc.kin">์ง์iN</a>
<a href="https://shopping.naver.com/" class="nav shop" data-clk="svc.shopping"><span class="blind">์ผํ</span></a>
<a href="https://shoppinglive.naver.com/home" class="nav shoplive" data-clk="svc.shoppinglive"><span class="blind">์ผํLIVE</span></a>
<a href="https://order.pay.naver.com/home" class="nav" data-clk="svc.pay">Pay</a>
<a href="https://tv.naver.com/" class="nav" data-clk="svc.tvcast"><i class="ico_tv"></i>TV</a>
<a href="https://dict.naver.com/" class="nav" data-clk="svc.dic">์ฌ์ </a>
<a href="https://news.naver.com/" class="nav" data-clk="svc.news">๋ด์ค</a>
<a href="https://finance.naver.com/" class="nav" data-clk="svc.stock">์ฆ๊ถ</a>
<a href="https://land.naver.com/" class="nav" data-clk="svc.land">๋ถ๋์ฐ</a>
<a href="https://map.naver.com/" class="nav" data-clk="svc.map">์ง๋</a>
<a href="https://vibe.naver.com/" class="nav" data-clk="svc.vibe">VIBE</a>
<a href="https://book.naver.com/" class="nav" data-clk="svc.book">์ฑ
</a>
<a href="https://comic.naver.com/" class="nav" data-clk="svc.webtoon">์นํฐ</a>
์ด๋ฒ์๋ ์๋ก์ด ์์๋ฅผ ์ถ๊ฐํด๋ณด์
HTML ์์ ์์ฑ/์ถ๊ฐ
document.createElement(ํ๊ทธ๋ช ) : HTML ์์ ์์ฑ
document.appendChild(์์) : HTML ์์ ์ถ๊ฐ (ํ๋ฉด์ ๋ฑ์ฅ)
์์
<!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>
<style>
.box1 {
width: 100px;
height: 100px;
border-radius: 5px;
line-height: 100px;
text-align: center;
}
</style>
</head>
<body>
<script>
const newBox1 = document.createElement("div") //div ์์ ์์ฑ
newBox1.innerText = "Hello" //๋ฌธ๊ตฌ ์ถ๊ฐ
newBox1.setAttribute("class","box1") //box1์ด๋ผ๋ ์ด๋ฆ์ผ๋ก ํด๋์ค ์ค์
newBox1.style.background = "blueviolet" //backgroundํ๋กํผํฐ ์ ์ฉ
document.body.appendChild(newBox1) //์์ ์ถ๊ฐ
</script>
</body>
</html>
HTML ์์ ์ญ์ /๋์ฒด
document.removeChild(์์) : HTML ์์ ์ญ์
document.replaceChile( ์๋ก์ด ๊ฒ, ๊ธฐ์กด์ ๊ฒ ) : HTML ์์ ๋์ฒด
์์
<!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>
<style>
.box1 {
width: 100px;
height: 100px;
border-radius: 5px;
line-height: 100px;
text-align: center;
}
.box2 {
width: 200px;
height: 200px;
border-radius: 100px;
line-height: 200px;
text-align: center;
}
</style>
</head>
<body>
<script>
const newBox1 = document.createElement("div");
newBox1.innerText = "Hello";
newBox1.setAttribute("class","box1");
newBox1.style.background = "blueviolet";
const newBox2 = document.createElement("div");
newBox2.innerText = "World";
newBox2.setAttribute("class","box2");
newBox2.style.background = "blueviolet";
document.body.appendChild(newBox1);
document.body.replaceChild(newBox2, newBox1); // ๋์ฒดํ๊ธฐ
</script>
</body>
</html>
HTML ์์ ํ์
element.parentNode : ๋ถ๋ชจ์์
element.nextElementSibling : ํ์ฌ ์์์ ๋ค์ ํ์ ์์
element.previousElementSibling : ํ์ฌ ์์์ ์ด์ ํ์ ์์
element.children : ์์ ์์๋ค(๋ฐฐ์ด๋ก ๋ฐํ)โ
'๐ | WEB DEV > Vanilla JS' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
JS_๋ฌธ๋ฒ (10)_๋ํ์ฐฝ (0) | 2022.07.26 |
---|---|
JS_๋ฌธ๋ฒ (9)_์ด๋ฒคํธ (0) | 2022.02.24 |
JS_๋ฌธ๋ฒ (7)_DOM / BOM (0) | 2022.02.23 |
JS_๋ฌธ๋ฒ (6)_promise ๋ฉ์๋(catch, finally, all, race ๋ฑ ) (0) | 2022.02.20 |
JS_๋ฌธ๋ฒ (5)_๋๊ธฐ/๋น๋๊ธฐ ์ฒ๋ฆฌ (0) | 2022.02.18 |
๋๊ธ