본문 바로가기
  • 사람은 무언가를 배울 필요가 있을때가 되서야 비로소 배우게 된다.
JavaScript

[금기]JS로 css스타일지정 한번에 !

by YoHaYo 2023. 12. 26.
728x90
반응형

 

<!DOCTYPE html>
<html lang="ko">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>JS연습</title>
</head>
<body>
  <ul id="gnb">
    <li></li>
    <li></li>
    <li></li>
    <li></li>
  </ul>
  <button onclick="inithtml()">사이트 폭파 버튼</button>
  <script>

  window.onload = init;

  const textcolor = [
    {
        color: "green",
        bg: "red",
        nm : "네이버"
    },
    {
        color: "yellow",
        bg: "darkblue",
        nm: "다음"
    },
    {
        color: "blue",
        bg: "green",
        nm: "쿠팡"
    },
    {
        color: "black",
        bg: "orange",
        nm: "구글"
    }
]

  function init() {
    for (x in textcolor) {
      insectlist(x, textcolor[x].nm, textcolor[x].color, textcolor[x].bg)
    }
    // insectlist(0,"구글");
    // insectlist(1,"네이버");
    // insectlist(2,"다음");
    // inithtml();
  }
  
  function insectlist(a, b, c, d)  {
    var listItem = document.querySelectorAll('#gnb li')[a];

    listItem.innerHTML = b;

    listItem.style = `
        color: ${c};
        background-color: ${d};
    `;
}

  function inithtml() {
    document.body.innerHTML = ''
  }
  </script>
</body>
</html>

 

→ 이렇게 백틱으로 js에서 스타일을 여러개 지정하면transition 줄때 시간차로 오류나는걸 확인함 !

원인은 style이라는 객체가 초기화되면서 나는 원인! 

하여튼 style은 귀찮아도 따로따로 쓰자 !

반응형

댓글