如何使用 LocalStorage 保存为按钮选择的背景颜色?

问题是我不知道如何使用 LocalStorage 为按钮保存选择的背景颜色。我以前从未使用过 LocalStorage,但我对代码的想法是以某种方式使用 myFunction(color),它利用了 onclick 函数中的颜色值。任何帮助,将不胜感激!


$("[data-toggle=popover]").popover

({

    html: true,

    sanitize: false,

    trigger: 'focus',

    content: function()

    {

      return $('#popover-content').html();

    }

  });

  let targetBtn;

  document.querySelectorAll('.myBtn').forEach((item) =>

  {

    item.addEventListener('click', (e) =>

    {

      targetBtn = e.target;

    })

  })

  function myFunction(color)

  {

    if (targetBtn)

    {

      targetBtn.style.background = color;

      /* Here I somehow want to use localStorage

         to save the picked colors for the buttons 

         localStorage.setItem('targetBtn', color); */

    }

  }

<head>

  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">

  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>

</head>

<body>

<button class="myBtn myBtnCorners1" data-toggle="popover" data-placement="bottom" data-html="true">1</button>

      <button class="myBtn" data-toggle="popover" data-placement="bottom" data-html="true">2</button>

  <div id="popover-content" class="hide">

    <button class="demo1" onclick="myFunction('red')">Red</button>

    <button class="demo2" onclick="myFunction('green')">Green</button>

    <button class="demo3" onclick="myFunction('blue')">Blue</button>

    <span class="close">&times;</span>

  </div>

 </body>


牛魔王的故事
浏览 202回答 3
3回答

茅侃侃

无法在代码段中进行测试,但代码应该很好。您将需要一个 Getter 和一个 Setter。拥有它们后,您可以获取、设置或加载所有首选项。var getColorPref = (i) => {&nbsp; return localStorage.getItem("color-" + i) || "";}var saveColorPref = (i, c) => {&nbsp; localStorage.setItem("color-" + i, c);&nbsp; return c;}var loadColorPrefs = () => {&nbsp; $(".myBtn").each(() => {&nbsp; &nbsp; var i = $(this).index();&nbsp; &nbsp; $(this).css("background", getColorPref(i));&nbsp; });}loadColorPrefs();var targetBtn;function setColor(c) {&nbsp; var i = $(targetBtn).index();&nbsp; console.log(i, c);&nbsp; $(targetBtn).css("background", saveColorPref(i, c));}$("[data-toggle=popover]").popover({&nbsp; html: true,&nbsp; sanitize: false,&nbsp; trigger: 'focus',&nbsp; content: function() {&nbsp; &nbsp; return $('#popover-content').html();&nbsp; }});$('.myBtn').each((i, item) => {&nbsp; $(item).click((e) => {&nbsp; &nbsp; targetBtn = e.target;&nbsp; });});.popover-content {&nbsp; display: flex;&nbsp; justify-content: space-around;&nbsp; align-items: center;&nbsp; background: #efefef;&nbsp; width: 230px;&nbsp; height: 80px;}.close {&nbsp; color: #aaaaaa;&nbsp; float: right;&nbsp; font-size: 28px;&nbsp; font-weight: bold;&nbsp; position: absolute;&nbsp; top: 0px;&nbsp; left: 210px;}.close:hover,.close:focus {&nbsp; color: #000;&nbsp; text-decoration: none;&nbsp; cursor: pointer;&nbsp; transition: 0.5s;}.myBtn {&nbsp; background-color: #DCDCDC;&nbsp; border: 0.5px solid #808080;&nbsp; color: white;&nbsp; width: 30px;&nbsp; height: 30px;&nbsp; border-radius: 6%;&nbsp; text-align: center;&nbsp; text-decoration: none;&nbsp; display: inline-block;&nbsp; font-size: 16px;}.demo1 {&nbsp; background-color: red;&nbsp; border: none;&nbsp; color: white;&nbsp; width: 60px;&nbsp; height: 60px;&nbsp; border-radius: 50%;&nbsp; text-align: center;&nbsp; text-decoration: none;&nbsp; display: inline-block;&nbsp; font-size: 16px;}.demo2 {&nbsp; background-color: green;&nbsp; border: none;&nbsp; color: white;&nbsp; width: 60px;&nbsp; height: 60px;&nbsp; border-radius: 50%;&nbsp; text-align: center;&nbsp; text-decoration: none;&nbsp; display: inline-block;&nbsp; font-size: 16px;}.demo3 {&nbsp; background-color: blue;&nbsp; border: none;&nbsp; color: white;&nbsp; width: 60px;&nbsp; height: 60px;&nbsp; border-radius: 50%;&nbsp; text-align: center;&nbsp; text-decoration: none;&nbsp; display: inline-block;&nbsp; font-size: 16px;}.hide {&nbsp; display: none;}<head>&nbsp; <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">&nbsp; <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>&nbsp; <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script></head><body>&nbsp; <button class="myBtn myBtnCorners1" data-toggle="popover" data-placement="bottom" data-html="true">1</button>&nbsp; <button class="myBtn" data-toggle="popover" data-placement="bottom" data-html="true">2</button>&nbsp; <div id="popover-content" class="hide">&nbsp; &nbsp; <button class="demo1" onClick="setColor('red')">Red</button>&nbsp; &nbsp; <button class="demo2" onClick="setColor('green')">Green</button>&nbsp; &nbsp; <button class="demo3" onClick="setColor('blue')">Blue</button>&nbsp; &nbsp; <span class="close">&times;</span>&nbsp; </div></body>

BIG阳

OP 明确询问有关保存到 localStorage 的问题,因此 Leira Sánchez 的回答是正确的。我相信 OP 也在询问从 localStorage 获取数据,所以我会详细说明。我建议向您的 HTML 按钮添加 id 以明确保存颜色:<button id="myBtn1" class="myBtn myBtnCorners1" ...>1</button><button id="myBtn2" class="myBtn"...>2</button>myFunction()现在应该能够获取targetBtn.id,并且您将保存到 localStorage 中,如下所示:function myFunction(color) {&nbsp; if (targetBtn) {&nbsp; &nbsp; targetBtn.style.background = color;&nbsp; &nbsp; localStorage.setItem(targetBtn.id, color);&nbsp; }}然后在您的 document.getQuerySelector 中,您可以从 localStorage 获取项目并应用样式&nbsp; document.querySelectorAll('.myBtn').forEach((item) => {&nbsp; &nbsp; const id = $(item).attr("id")&nbsp; &nbsp; $(item).css("background-color", localStorage.getItem(id));&nbsp; &nbsp; item.addEventListener('click', (e) => {&nbsp; &nbsp; &nbsp; targetBtn = e.target;&nbsp; &nbsp; })&nbsp; })

三国纷争

您可以通过以下方式将某些内容保存到本地存储:localStorage.setItem('whatever you wanna save goes here');https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript