Roel Notebook

[JS] DOM APIs 연습하기

by Roel Downey
728x90
반응형

- 마지막에 strawberry 뒤에 망고 추가해보기 

<결과>

html 코드 

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
  <h1>selector test</h1>
  
  <ul>
    <li>apple</li>
    <li>orange</li>
    <li>banana</li>
    <li>grape</li>
    <li>strawberry</li>
  </ul>

</body>
</html>

자바스크립트 정답

더보기
var mango = document.createElement("li");
var mangoText = document.createTextNode("망고");
mango.appendChild(mangoText);

var parent = document.querySelector("ul");
parent.appendChild(mango);

 

 

- insertBefore메서드를 사용해서, orange와  banana 사이에 망고 추가해보기 

<결과>

html 코드 

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
  <h1>selector test</h1>
  
  <ul>
    <li>apple</li>
    <li>orange</li>
    <li>banana</li>
    <li>grape</li>
    <li>strawberry</li>
  </ul>

</body>
</html>

 

자바스크립트 정답

더보기
var mango = document.createElement("li");
var mangoText = document.createTextNode("망고");
mango.appendChild(mangoText);

var parent = document.querySelector("ul");
var banana = document.querySelector("li:nth-child(3)");

parent.insertBefore(mango,banana);

 

 

- insertAdjacentHTML메서드를 사용해서, orange와 banana 사이에 망고 추가해보기 

<결과>

html 코드 

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
  <h1>selector test</h1>
  
  <ul>
    <li>apple</li>
    <li>orange</li>
    <li>banana</li>
    <li>grape</li>
    <li>strawberry</li>
  </ul>

</body>
</html>

 

자바스크립트 정답

더보기
var orange = document.querySelector("li:nth-child(2)");
orange.insertAdjacentHTML('afterend',"<li>망고</li>");

 

insertAdjacentHTML 문제에서 공부 해야할 부분!! 

링크 : https://developer.mozilla.org/ko/docs/Web/API/Element/insertAdjacentHTML

- apple을 grape 와 strawberry 사이로 옮기기 

<결과>

 

html 코드 

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
  <h1>selector test</h1>
  
  <ul>
    <li>apple</li>
    <li>orange</li>
    <li>banana</li>
    <li>grape</li>
    <li>strawberry</li>
  </ul>

</body>
</html>

 

자바스크립트 정답

더보기
var strawberry = document.querySelector("li:nth-child(5)");
var apple = document.querySelector("li:nth-child(1)");

var parent = document.querySelector("ul");
parent.insertBefore(apple,strawberry);

 

- class 가 'red'인 노드만 삭제 하기 

<결과>

html 코드 

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
  <h1>selector test</h1>
  
  <ul>
    <li class="red">apple</li>
    <li class="red">orange</li>
    <li>banana</li>
    <li>grape</li>
    <li>strawberry</li>
  </ul>

</body>
</html>

자바스크립트 정답

더보기
var reds = document.querySelectorAll("li.red");
var parent = document.querySelector("ul");

for(var i = 0 ; i<reds.length; i++) {
  parent.removeChild(reds[i]);
}

 

- section 태그의 자손 중에 blue라는 클래스를 가지고 있는 노드가 있다면, 그 하위에 있는 h2 노드를 삭제 하기 

<결과>

html 코드 

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
  <h1>selector test</h1>
  
    
<section>
  <h2> red section </h2>
  <ul>
    <li class="red">apple</li>
    <li class="red">orange</li>
    <li>banana</li>
    <li>grape</li>
    <li>strawberry</li>
  </ul>
</section>
  
  <Br>
  
<section>
  <h2> blue section </h2>
  <ul>
    <li class="green blue">apple</li>
    <li class="red">orange</li>
    <li>banana</li>
    <li>grape</li>
    <li>strawberry</li>
  </ul>
</section>

</body>
</html>

자바스크립트 정답

더보기
var blueNode = document.querySelectorAll("section .blue");

Array.from(blueNode).forEach((function (v) {
  var section = v.parentNode.parentNode;
  var h2 = section.querySelector("h2");
  section.removeChild(h2);
}));

 

 

 

 

 

728x90
반응형

블로그의 정보

What doing?

Roel Downey

활동하기