【问题标题】:make javascript function saved保存javascript函数
【发布时间】:2021-10-17 23:32:48
【问题描述】:

我正在使用 javascript 函数来更改带有数据集的某些图标的颜色。 我不知道如何使我通过单击更改的颜色保持这种状态。

这是javascript函数:

window.onload = oppdater;

function oppdater() {

   const clickHandler = function () {
        this.style.color = 
          this.style.color != this.dataset.activeColor ?
          this.dataset.activeColor :
          this.dataset.disabledColor;
      };
    
    for (const element of document.querySelectorAll(".active-color-aware")) {
      element.onclick = clickHandler;
    }

}
<div>
<img src="img/mug_test.png" height="200px" width="200px">
            <p>fillifjonka grå</p>
            <div id="icon_colors" class="center_icon_text">
            <i data-active-color="green" data-disabled-color="#6080a0" class="fas fa-home fa-3x active-color-aware"></i>
            <i data-active-color="yellow" data-disabled-color="#6080a0" class="fas fa-grin-hearts fa-3x active-color-aware"></i>
            <i data-active-color="red" data-disabled-color="#6080a0" class="fas fa-heart-broken fa-3x active-color-aware"></i>
            </div>  
        </div>

【问题讨论】:

    标签: javascript html web save


    【解决方案1】:

    如果你想在用户关闭浏览器后保存信息事件,你可以利用localstorage

    // Save
    localStorage.setItem("activeColor", "red");
    // Get
    var activeColor = localStorage.getItem("activeColor");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-11-05
      • 2020-06-17
      • 2014-04-13
      • 1970-01-01
      • 2020-11-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多