Javascript更改css代码

问题描述 投票:1回答:2

我有一个Javascript代码,用颜色选择器更改侧边栏颜色

var colorWell;
var defaultColor = "#0078c0";

window.addEventListener("load", startup, false);
function startup() {
  colorWell = document.querySelector("#colorWell");
  colorWell.value = defaultColor;
  colorWell.addEventListener("input", updateFirst, false);
  colorWell.addEventListener("change", updateAll, false);
  colorWell.select();
}
function updateFirst(event) {
  var side = document.querySelector(".sidebar");

  if (side) {
    side.style.backgroundColor = event.target.value;
  }
}function updateAll(event) {
  document.querySelectorAll(".sidebar").forEach(function(side) {
    side.style.backgroundColor = event.target.value;
  });
}

有没有办法让这段代码也改变css文件中的css值?

javascript html css
2个回答
0
投票

我想如果你想让改变颜色的用户在返回网站时看到它,我会用window.localStorage保存颜色数据。

有一个工作小提琴会很高兴


0
投票

是的你可能会:

© www.soinside.com 2019 - 2024. All rights reserved.