[Js] ์๋ณ์ API | ์กฐํ API | ์์ฑ API
- -
๋ชจ๋ ํฌ์คํ ์ ์ธํ๋ฐ [์ํ์ฝ๋ฉ] ์๋ฐ์คํฌ๋ฆฝํธ ๊ฐ์์ ๋ด์ฉ์ ๋ด๊ณ ์์ผ๋ฉฐ
์ถ์ฒ๋ ์๋์ ์ฃผ์๋ก "egoing Lee"๋์ ์๋ฃ์ ๋๋ค.
์์ ์ ์ธ ์๋๊ฐ ์๋ ๊ณต๋ถํ ๊ฒ์ ์ ๋ฆฌํด๋๋ ๋ชฉ์ ์ผ๋ก ํฌ์คํ ํ ๊ฒ์ ๋๋ค.
์น๋ธ๋ผ์ฐ์ Javascript (์๋ฐ์คํฌ๋ฆฝํธ) - ์ธํ๋ฐ
์ํ์ฝ๋ฉ ์๋ฐ์คํฌ๋ฆฝํธ ๊ฐ์ข ์ ๋๋ค. ์น๋ธ๋ผ์ฐ์ ๋ฅผ ์ง์ ์ ์ดํ ์ ์๋ ์๋ฐ์คํฌ๋ฆฝํธ ๋ ํผ๋ฐ์ค๋ฅผ ๊ณต๋ถํด ๋ด ๋๋ค. ์ด๊ธ ์น ๊ฐ๋ฐ ํ๋ก๊ทธ๋๋ฐ ์ธ์ด JavaScript ์จ๋ผ์ธ ๊ฐ์ ์๋ฐ์คํฌ๋ฆฝํธ ๊ฐ์ข
www.inflearn.com
์ด๋ฒ ํฌ์คํ ์ ๊ฐ์์ "Element ๊ฐ์ฒด" ์ค ์๋ณ์ API, ์กฐํ API, ์์ฑ API ๊ฐ์๋ฅผ ๋ชจ๋ ํฌํจํ์ฌ ์์ฑํ์์ต๋๋ค.
์๋ณ์ API
์๋ฆฌ๋จผํธ๋ฅผ ์ ์ดํ๊ธฐ ์ํด์๋ ๊ทธ ์๋ฆฌ๋จผํธ๋ฅผ ์กฐํํ๊ธฐ ์ํ ์๋ณ์๊ฐ ํ์ํ๋ค. ๋ณธ ์์ ์์๋ ์๋ณ์ API๋ค์ ๋ํด์ ์์๋ณด์.
HTML์์ ์๋ฆฌ๋จผํธ์ ์ด๋ฆ๊ณผ id ๊ทธ๋ฆฌ๊ณ class๋ ์๋ณ์๋ก ์ฌ์ฉ๋๋ค. ์๋ณ์ API๋ ์ด ์๋ณ์๋ฅผ ๊ฐ์ ธ์ค๊ณ ๋ณ๊ฒฝํ๋ ์ญํ ์ ํ๋ค.
Element.tagName
ํด๋น ์๋ฆฌ๋จผํธ์ ํ๊ทธ ์ด๋ฆ์ ์์๋ธ๋ค. ํ๊ทธ ์ด๋ฆ์ ๋ณ๊ฒฝํ์ง๋ ๋ชปํ๋ค.(์ฆ ์ฝ๊ธฐ ์ ์ฉ์ ๋ณ๊ฒฝํด๋ ๋ค์ ๋์์ด)
<ul>
<li>html</li>
<li>css</li>
<li id="active" class="important current">JavaScript</li>
</ul>
<script>
console.log(document.getElementById('active').tagName)
</script>
Element.id
๋ฌธ์์์ id๋ ๋จ ํ๋๋ง ๋ฑ์ฅํ ์ ์๋ ์๋ณ์๋ค. ์๋ ์์ ๋ id์ ๊ฐ์ ์ฝ๊ณ ๋ณ๊ฒฝํ๋ ๋ฐฉ๋ฒ์ ๋ณด์ฌ์ค๋ค.
<ul>
<li>html</li>
<li>css</li>
<li id="active">JavaScript</li>
</ul>
<script>
var active = document.getElementById('active');
console.log(active.id);
active.id = 'deactive';
console.log(active.id);
</script>
์์ฒ๋ผ ์ ๋ ฅํ๋ฉด id๊ฐ์ด active์์ deactive๋ก ๋ณ๊ฒฝ๋๋ค. ์ฝ์์ฐฝ์ผ๋ก ์ถ๋ ฅํด๋ณด๋ฉด ๋ฐ๋ ๊ฒฐ๊ณผ๊ฐ ์ถ๋ ฅ๋๋ค.
Element.className
ํด๋์ค๋ ์ฌ๋ฌ๊ฐ์ ์๋ฆฌ๋จผํธ๋ฅผ ๊ทธ๋ฃนํํ ๋ ์ฌ์ฉํ๋ค. ์ด ๋งค์๋ ๋ณด๋ค๋ ์๋ ํํ์ ๋ ๋ง์ด ์ด๋ค.
<ul>
<li>html</li>
<li>css</li>
<li id="active">JavaScript</li>
</ul>
<script>
var active = document.getElementById('active');
// class ๊ฐ์ ๋ณ๊ฒฝํ ๋๋ ํ๋กํผํฐ์ ์ด๋ฆ์ผ๋ก className์ ์ฌ์ฉํ๋ค.
active.className = "important current";
console.log(active.className);
// ํด๋์ค๋ฅผ ์ถ๊ฐํ ๋๋ ์๋์ ๊ฐ์ด ๋ฌธ์์ด์ ๋ํ๋ค.
active.className += " readed"
</script>
์๋ฐ์คํฌ๋ฆฝํธ์์ ์์ฝ์ด์ธ ๊ฒฝ์ฐ์ html ์์ฑ์ด๋ฆ๊ณผ ํ๋กํผํฐ ์ด๋ฆ์ด ์ผ์นํ์ง ์๋ ๊ฒฝ์ฐ๊ฐ ๋ง๋ค.
Element.classList
className์ ๋นํด์ ํจ์ฌ ํธ๋ฆฌํ ์ฌ์ฉ์ฑ์ ์ ๊ณตํ๋ค. (ํธ๋ฆฌํ๋งํผ ์ฌ์ฉํ๊ธฐ๊ฐ ์ข ๊น๋ค๋ก์)
<ul>
<li>html</li>
<li>css</li>
<li id="active" class="important current">JavaScript</li>
</ul>
<script>
function loop(){
for(var i=0; i<active.classList.length; i++){
console.log(i, active.classList[i]);
}
}
// ํด๋์ค๋ฅผ ์ถ๊ฐ
</script>
<input type="button" value="DOMTokenList" onclick="console.log(active.classList);" />
<input type="button" value="์กฐํ" onclick="loop();" />
<input type="button" value="์ถ๊ฐ" onclick="active.classList.add('marked');" />
<input type="button" value="์ ๊ฑฐ" onclick="active.classList.remove('important');" />
<input type="button" value="ํ ๊ธ" onclick="active.classList.toggle('current');" />
classList๋ฅผ ํตํด add, remove๊ฐ ๊ฐ๋ฅํ๊ณ , ์์ ๋ ์์ ๊ณ ์์ ๋ ์๊ธฐ๊ฒ ํ๊ณ ์ถ๋ค๋ฉด toggle์ ์ฌ์ฉํ๋ฉด ๋๋ค.
์์ธํ ๋ด์ฉ์ ์๋ ์์์ ์ฐธ๊ณ ํด์ ๋ค์ ํ๋ฒ ์ดํด๋ณด๋๋ก ํ์!
www.youtube.com/watch?v=9XyowP9VjDo&feature=youtu.be
์กฐํ API
์ง๊ธ๊น์ง document.getElementsBy* ๋ฉ์๋๋ฅผ ํตํด์ ์๋ฆฌ๋จผํธ๋ฅผ ์กฐํํ๋ค. document ๊ฐ์ฒด๋ ๋ฌธ์ ์ ์ฒด๋ฅผ ์๋ฏธํ๋ ์๋ฆฌ๋จผํธ์ด๊ธฐ ๋๋ฌธ์ document์ ์กฐํ ๋ฉ์๋๋ ๋ฌธ์ ์ ์ฒด๋ฅผ ๋์์ผ๋ก ์๋ฆฌ๋จผํธ๋ฅผ ์กฐํํ๋ค.
Element ๊ฐ์ฒด ์ญ์๋ getElementsBy* ์๋ฆฌ๋จผํธ๋ฅผ ๊ฐ์ง๊ณ ์๋๋ฐ Element ๊ฐ์ฒด์ ์กฐํ ๋ฉ์๋๋ ํด๋น ์๋ฆฌ๋จผํธ์ ํ์ ์๋ฆฌ๋จผํธ๋ฅผ ๋์์ผ๋ก ์กฐํ๋ฅผ ์ํํ๋ค.
<!DOCTYPE html>
<html>
<head>
<title> Object ํ์ฅ</title>
</head>
<body>
<ul>
<li class = "marked">html</li>
<li>css</li>
<li id="active">Javascript
<ul>
<li>JavaScript Core</li>
<li class="marked">DOM</li>
<li class="marked">BOM</li>
</ul>
</li>
</ul>
<script>
var list = document.getElementsByClassName('marked');
console.group('document');//๊ทธ๋ฃน document
for(var i = 0; i<list.length; i++){
console.log(list[i].textContent);
}
console.groupEnd(); //document๊ทธ๋ฃน ๋
console.group('active'); //๊ทธ๋ฃน์ด๋ฆ
var active = document.getElementById('active'); //active๋ผ๋ ๊ฐ์ฒด๋ก ๋ด์. ์ฆ ์ฌ๊ธฐ์ ํด๋นํ๋ ํ์๋ง list์ ๋ด๊ธฐ๊ฒ ๋จ
var list = active.getElementsByClassName('marked');//active๊ฐ์ฒด์ ํ์ ๊ฐ์ฒด๋ค ์ค class๊ฐ marked์ธ ๊ฒ!
for(var i = 0; i<list.length; i ++){
console.log(list[i].textContent);
}
console.groupEnd();
</script>
</body>
</html>
์คํ๊ฒฐ๊ณผ
document๊ฐ์ฒด์ getElement.. ์ element ๊ฐ์ฒด์ getElement ..๊ฐ ๋ค๋ฅด๊ฒ ์ถ๋ ฅ๋๋ ๊ฒ์ ํ์ธ ํ ์ ์๋ค.
์ฆ document.getElementsByClassName์ผ๋ก ์กฐํ๋ฅผ ํ๋ฉด ์ ์ฒด ๋ฌธ์์์ ์กฐํ๊ฐ ๋๋ ๊ฒ์ด๊ณ ,
ํน์ ํ element์ getElements ๋ฉ์๋๋ฅผ ์ฌ์ฉํ๊ฒ ๋๋ฉด ๊ทธ ๊ฐ์ฒด์ ํ์ ์๋ฆฌ๋จผํธ๋ค๋ง ์กฐํํ๊ฒ ๋๋ ๊ฒ์ด๋ค.
์์ฑ API
์์ฑ์ HTML์์ ํ๊ทธ๋ช ๋ง์ผ๋ก๋ ๋ถ์กฑํ ๋ถ๊ฐ์ ์ธ ์ ๋ณด๋ผ๊ณ ํ ์ ์๋ค. ์ด ์์ฑ์ ์ด๋ป๊ฒ ์ ์ดํ๋๊ฐ ์์๋ณด์.
์์ฑ์ ์ ์ดํ๋ API๋ ์๋์ ๊ฐ๋ค. ๊ฐ๊ฐ์ ๊ธฐ๋ฅ์ ์ด๋ฆ์ ํตํด์ ์ถฉ๋ถํ ์ ์ถํ ์ ์์ ๊ฒ์ด๋ค.
- Element.getAttribute(name)
- Element.setAttribute(name, value)
- Element.hasAttribute(name); //ํด๋นํ๋ ์์ฑ ๊ฐ์ด ์๋์ง ์๋์ง ์ฌ๋ถ๋ฅผ ์๋ ค์ค.
- Element.removeAttribute(name);
<a id="target" href="http://opentutorials.org">opentutorials</a>
<script>
var t = document.getElementById('target');
console.log(t.getAttribute('href')); //http://opentutorials.org
t.setAttribute('title', 'opentutorials.org'); // title ์์ฑ์ ๊ฐ์ ์ค์ ํ๋ค.
console.log(t.hasAttribute('title')); // true, title ์์ฑ์ ์กด์ฌ์ฌ๋ถ๋ฅผ ํ์ธํ๋ค.
t.removeAttribute('title'); // title ์์ฑ์ ์ ๊ฑฐํ๋ค.
console.log(t.hasAttribute('title')); // false, title ์์ฑ์ ์กด์ฌ์ฌ๋ถ๋ฅผ ํ์ธํ๋ค.
</script>
๊ฐ์ ๋ณ์๋ก ๋ด์์ ๊ฐ์ฒด์ ํด๋นํ๋ ๊ฐ์ get๋ฉ์๋๋ฅผ ํตํด ๊ฐ์ ธ์ฌ ์๋ ์๊ณ , set ๋ฉ์๋๋ฅผ ์ด์ฉํด์ ์์ฑ์ ํด๋นํ๋ ๊ฐ์ ๋ณ๊ฒฝํ ์๋ ์๋ค.
์์ฑ๊ณผ ํ๋กํผํฐ
์์๊ณผ ํ๋กํผํฐ๋ ์กฐ๊ธ ์ฐจ์ด๊ฐ ์๋ค. ์ด๋ ํ ์ฐจ์ด๊ฐ ์๋์ง ์์๋ณด๋๋ก ํ์.
๋ชจ๋ ์๋ฆฌ๋จผํธ์ (HTML)์์ฑ์ (JavaScript ๊ฐ์ฒด์) ์์ฑ๊ณผ ํ๋กํผํฐ๋ก ์ ์ด๊ฐ ๊ฐ๋ฅํ๋ค. ์์ ๋ฅผ ๋ณด์.
<p id="target">
Hello world
</p>
<script>
var target = document.getElementById('target');
// attribute ๋ฐฉ์์. attribute์ ์ ๊ทผ ํ๋ ๊ฒ์ด ์์ฑ๋ฐฉ์!
target.setAttribute('class', 'important');
// property ๋ฐฉ์
target.className = 'important';
</script>
setAttribute('class', 'important')์ className = 'important'๋ ๊ฐ์ ๊ฒฐ๊ณผ๋ฅผ ๋ง๋ ๋ค.
ํ์ง๋ง ์ ์๋ attribute ๋ฐฉ์(์์ฑ์ด๋ผ๊ณ ๋ถ๋ฅด๊ฒ ๋ค)์ด๊ณ ํ์๋ property ๋ฐฉ์์ด๋ค. property ๋ฐฉ์์ ์ข ๋ ๊ฐํธํ๊ณ ์๋๋ ๋น ๋ฅด์ง๋ง ์ค์ html ์์ฑ์ ์ด๋ฆ๊ณผ ๋ค๋ฅธ ์ด๋ฆ์ ๊ฐ๋ ๊ฒฝ์ฐ๊ฐ ์๋ค. ๊ทธ๊ฒ์ ์๋ฐ์คํฌ๋ฆฝํธ์ ์ด๋ฆ ๊ท์น ๋๋ฌธ์ด๋ค.
ํ๋กํผํฐ ๋ฐฉ์์ ๊ฒฝ์ฐ ๋ช๊ฐ์ง ์์ธ๊ฐ ์๊ธฐ ๋๋ฌธ์ ๊ธฐ์ตํ๊ณ ์ฌ์ฉํด์ผ ํ๋ค.
์ฌ์ง์ด ์์ฑ๊ณผ ํ๋กํผํฐ๋ ๊ฐ์ด ๋ค๋ฅผ์๋ ์๋ค. ์๋ ์ฝ๋๋ฅผ ์คํํ ๊ฒฐ๊ณผ๋ ์์ฑ๊ณผ ํ๋กํผํฐ์ ๊ฐ์ด ๊ผญ ๊ฐ์ ๊ฒ์ ์๋๋ผ๋ ๊ฒ์ ๋ณด์ฌ์ค๋ค.
<a id="target" href="./demo1.html">ot</a>
<script>
//ํ์ฌ ์นํ์ด์ง๊ฐ http://localhost/webjs/Element/attribute_api/demo3.html ์ผ ๋
var target = document.getElementById('target');
// http://localhost/webjs/Element/attribute_api/demo1.html
console.log('target.href', target.href);
// ./demo1.html
console.log('target.getAttribute("href")', target.getAttribute("href"));
</script>
๋ฏธ๋ฌํ๊ฒ ๋ค๋ฅธ ๋ฌธ์ ๋ค์ด ์์ผ๋, ๊ฐ๊ฐ ์ํฉ์ ๋ง๊ฒ ์ฌ์ฉํ๋ ๊ฒ์ด ์ข์ ๋ฐฉ๋ฒ์ด๋ค.
'Languages > Java Script' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Js] Node ๊ฐ์ฒด | Node ๊ด๊ณ API (0) | 2021.02.10 |
---|---|
[Js] jQuery ์์ฑ ์ ์ด API | jQuery ์กฐํ ๋ฒ์ ์ ํ | javascript (0) | 2021.02.10 |
[Js] Element ๊ฐ์ฒด | Node | ์๋ณ์ | ์กฐํ | ์๋ฆฌ๋จผํธ (0) | 2021.02.08 |
[Js] jQuery ๊ฐ์ฒด | map() | jQuery API (0) | 2021.02.08 |
์์คํ ๊ณต๊ฐ ๊ฐ์ฌํฉ๋๋ค