📜  如何将主题属性添加到 :root - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:02:41.833000             🧑  作者: Mango

代码示例1
const toggleBtn = document.querySelector("#toggle-theme");
toggleBtn.addEventListener('click', e => {
  console.log("Switching theme");
  if(document.documentElement.hasAttribute('theme')){
    document.documentElement.removeAttribute('theme');
  }
  else{
    document.documentElement.setAttribute('theme', 'dark');
  }
});