JS移动端问题:关于如何保持按钮禁用状态和刷新后保持禁用不变

具体js代码和css基础样式下面有,要实现的效果就是点击提货按钮后即使再次刷新页面,提货按钮样式也是保持上次点击后的样子,即下面这样:
1.点击前

img


2.点击后

img

我原来想的是通过本地存储点击过的不同的提货按钮编号来找到这个按钮,然后再它被点击之后重新渲染页面,再通过这个编号index来单独设置它的样式已经放在渲染函数里面了,在二次渲染的后面,结果发现只有二次渲染执行了,更改的样式并没有被执行,请教下怎么解决这两个问题:
1.实现提货按钮按下后再次刷新也能保持禁用状态不变
2.怎么样可以让用户只能点击一次提货按钮且刷新后保持不能按下按钮触发?



```javascript

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta
      name="viewport"
      content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <link
      rel="stylesheet"
      href="./css/common.css" />
    <link
      rel="stylesheet"
      href="./css/task.css" />
    <script src="./js/flexible.js"></script>
  </head>
  <body>
    <!-- 1. header1头部公共区域 -->
    <div class="header1">
      <div
        class="father"
        data-id="0">
        待提货
        <p class="line"></p>
      </div>
      <div
        class="father"
        data-id="1">
        在途
        <p class="line"></p>
      </div>
      <div
        class="father"
        data-id="2">
        已完成
        <p class="line"></p>
      </div>
    </div>
    <!-- 3. 主体区域 -->
    <div class="container">
      <div class="task">
        <div class="title">
          <p>任务编号: XAHH1234567</p>
          <span>已延迟</span>
        </div>
        <div class="main">
          <div class="address">
            <p class="from"></p>
            <div class="sline"></div>
            <p class="to"></p>
          </div>
          <div class="right">
            <p class="from">北京市昌平区回龙观街道西三旗桥东金燕龙<br />写字楼8877号</p>
            <p class="to">河南省郑州市路北区北清路99号</p>
          </div>
        </div>
        <div class="bottom">
          <div class="bleft">
            <p>提货时间</p>
            <p class="time">2022.05.04 13:00</p>
          </div>
          <div class="bright">提货</div>
        </div>
      </div>
    </div>
    <!-- 2. footer底部公共区域 -->
    <div class="footer">
      <a href="javascript:;"
        ><img
          src="./assests/first.png"
          alt="" />
        <p>任务</p></a
      >
      <a href="javascript:;"
        ><img
          src="./assests/second.png"
          alt="" />
        <p>消息</p></a
      >
      <a href="javascript:;"
        ><img
          src="./assests/last.png"
          alt="" />
        <p>我的</p></a
      >
    </div>
    <script>
      let indexArr = []

      const info = [
        {
          num: 'XAHH1234567',
          from: '北京市昌平区回龙观街道西三旗桥东金燕龙写字楼 8877 号',
          to: '河南省郑州市路北区北清路 99 号',
          time: '2022.05.04 13:00:00',
        },
        {
          num: 'YBHJ1234567',
          from: '上海市浦东新区世纪大道 123 号金贸大厦 1234 室',
          to: '广东省深圳市南山区科技中一路 99 号',
          time: '2022.05.05 14:00:00',
        },
        {
          num: 'ZXCS1234567',
          from: '杭州市西湖区文一西路 123 号中关村科技大厦 8877 室',
          to: '江苏省南京市江北新区江北大道 99 号',
          time: '2022.05.06 15:00:00',
        },
        {
          num: 'ABCD1234567',
          from: '成都市青羊区蜀都大道 123 号环球中心 1234 室',
          to: '重庆市江北区观音桥街道建新东路 99 号',
          time: '2022.05.07 16:00:00',
        },
        {
          num: 'EFGH1234567',
          from: '天津市滨海新区泰达街道第四大街 123 号滨海金融大厦 1234 室',
          to: '河北省石家庄市鹿泉区北站街 99 号',
          time: '2022.05.08 17:00:00',
        },
      ]
      const header1 = document.querySelector('.header1')
      const lines = document.querySelectorAll('.line')
      header1.addEventListener('touchstart', function (e) {
        if ((e.target.tagName = 'DIV')) {
          let num = e.target.dataset.id
          lines.forEach(function (item) {
            item.style.width = '0px'
            item.parentNode.style.color = '#818181'
          })
          lines[num].style.width = '23px'
          e.target.style.color = '#2a2929'
        }
      })
      const container = document.querySelector('.container')
      setContainer()
      function setContainer() {
        const arr = info.map(function (item) {
          return `
        <div class="task">
      <div class="title">
        <p>任务编号: ${item.num}</p>
        <span>已延迟</span>
      </div>
      <div class="main">
        <div class="address">
          <p class="from"></p>
          <div class="sline"></div>
          <p class="to"></p>
        </div>
        <div class="right">
          <p class="from">${item.from}</p>
          <p class="to">${item.to}</p>
        </div>
      </div>
      <div class="bottom">
        <div class="bleft">
          <p>提货时间</p>
          <p class="time">${item.time}</p>
        </div>
        <div class="bright">提货</div>
      </div>
    </div>       
        `
        })

        container.innerHTML = arr.join('')
        const brights = document.querySelectorAll('.bright')
        brights.forEach(function (item, index) {
          item.addEventListener('touchstart', function () {
            indexArr.push(index)
            localStorage.setItem('indexs', JSON.stringify(indexArr))
            let arr = JSON.parse(localStorage.getItem('indexs'))
            // setContainer()
            this.style.backgroundColor='#FADCD9'
            console.log(arr)
          })
        })

      }
    </script>
  </body>
</html>


```css

/* 去除常见标签默认的 margin 和 padding */
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}
/* 设置网页统一的字体大小、行高、字体系列相关属性 */
body {
  font: 16px/1.5 "Microsoft Yahei", "Hiragino Sans GB", "Heiti SC", "WenQuanYi Micro Hei", sans-serif;
  background-color: #F4F4F4;
}
/* 去除列表默认样式 */
ul,
ol {
  list-style: none;
}
/* 去除默认的倾斜效果 */
em,
i {
  font-style: normal;
}
/* 去除a标签默认下划线,并设置默认文字颜色 */
a {
  text-decoration: none;
  color: #333;
}
/* 设置img的垂直对齐方式为居中对齐,去除img默认下间隙 */
img {
  width: 100%;
  height: 100%;
  vertical-align: middle;
}
/* 去除input默认样式 */
input {
  border: none;
  outline: none;
  color: #333;
}
h1,
h2,
h3,
h4,
h5,
h6 {
  font-weight: 400;
}
/*  清除浮动 */
.clearfix::before,
.clearfix::after {
  content: '';
  display: block;
}
.clearfix::after {
  clear: both;
}
.container {
  width: 9.49333333rem;
  margin: 0 auto;
}
.header1 {
  padding: 0 0.82666667rem;
  width: 100%;
  height: 1.33333333rem;
  background-color: #fff;
  display: flex;
  align-items: center;
  position: fixed;
  left: 0;
  top: 0;
}
.header1 .father {
  font-size: 0.42666667rem;
  margin-right: 0.96rem;
  color: #818181;
  position: relative;
  white-space: nowrap;
}
.header1 .father:first-child {
  color: #2a2929;
}
.header1 .father:first-child .line {
  width: 0.61333333rem;
}
.header1 .father .line {
  width: 0;
  height: 0.10666667rem;
  background-color: #ef4f3f;
  position: absolute;
  bottom: -0.16rem;
  left: 50%;
  transform: translateX(-50%);
  border-radius: 0.05333333rem;
  transition: all 0.3s;
}
body {
  padding: 1.33333333rem 0.4rem 0;
}
body .container {
  width: 100%;
  height: 100%;
  margin-bottom: 1.73333333rem;
}
body .container .task {
  width: 9.2rem;
  height: 6.10666667rem;
  margin: 0.4rem 0;
  padding: 0.4rem 0.34666667rem 0 0.50666667rem;
  border-radius: 0.26666667rem;
  background-color: #fff;
}
body .container .task .title {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
body .container .task .title p {
  font-size: 0.42666667rem;
  color: #2a2929;
}
body .container .task .title span {
  display: none;
  font-size: 0.32rem;
  border: 0.02666667rem solid #ef4f3f;
  width: 1.38666667rem;
  height: 0.58666667rem;
  color: #ef4f3f;
  border-radius: 0.29333333rem;
  text-align: center;
  line-height: 0.53333333rem;
}
body .container .task .main {
  display: flex;
  margin: 0.58666667rem 0;
}
body .container .task .main .address {
  display: flex;
  flex-direction: column;
  height: 1.97333333rem;
  width: 0.58666667rem;
  justify-content: space-between;
}
body .container .task .main .address .sline {
  flex: 1;
  width: 0.02666667rem;
  border: 0.02666667rem dashed #eee;
  margin: 0 auto;
}
body .container .task .main .address p {
  color: #fff;
  text-align: center;
  line-height: 0.58666667rem;
  font-size: 0.32rem;
  border-radius: 50%;
  width: 0.58666667rem;
  height: 0.58666667rem;
}
body .container .task .main .address p:nth-child(1) {
  background-color: #2a2929;
}
body .container .task .main .address p:last-child {
  background-color: #ef4f3f;
}
body .container .task .main .right {
  margin-left: 0.29333333rem;
  flex: 1;
}
body .container .task .main .right p {
  font-size: 0.37333333rem;
  color: #818181;
}
body .container .task .main .right p:nth-child(1) {
  margin-bottom: 0.29333333rem;
}
body .container .task .bottom {
  padding-top: 0.37333333rem;
  border-top: 0.02666667rem solid #eee;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
body .container .task .bottom .bleft p {
  font-size: 0.37333333rem;
}
body .container .task .bottom .bleft p:first-child {
  color: #818181;
}
body .container .task .bottom .bleft p:last-child {
  color: #2a2929;
}
body .container .task .bottom .bright {
  width: 2.13333333rem;
  height: 0.85333333rem;
  border-radius: 0.42666667rem;
  background-color: #ef4f3f;
  font-size: 0.42666667rem;
  color: #fff;
  text-align: center;
  line-height: 0.85333333rem;
}
/* 
p{
  &.acitve
} 
双选加类选择器
*/
/* 
p{
& .active
}
子代选择器 .active
*/

提货后数据库应该有相应字段保存提货状态吧,生成页面的时候根据这个判断是否禁用。。

【以下回答由 GPT 生成】

解决方案

  1. 为了实现按钮点击后再次刷新页面时保持禁用状态不变,可以使用本地存储来记录按钮的点击状态。在按钮被点击后,将按钮的状态存储到本地存储中。在页面加载时,检查本地存储是否存在对应按钮的点击状态,并设置按钮的样式。
// 检查本地存储是否存在被点击按钮的状态
var clicked = localStorage.getItem('clicked');
if (clicked) {
  // 根据按钮的状态设置样式
  var father = document.querySelector('.father');
  father.classList.toggle('disabled', clicked === 'true');
}

// 监听按钮的点击事件
var father = document.querySelector('.father');
father.addEventListener('click', function () {
  // 设置按钮的点击状态为禁用
  this.classList.toggle('disabled', true);
  // 将按钮的状态存储到本地存储中
  localStorage.setItem('clicked', 'true');
});
  1. 如果需要确保用户只能点击一次提货按钮,并在刷新页面后保持按钮的禁用状态,可以在按钮被点击后,除了设置按钮的样式和本地存储按钮的点击状态外,还可以通过给按钮添加禁用属性来确保用户无法再次点击按钮。
// 检查本地存储是否存在被点击按钮的状态
var clicked = localStorage.getItem('clicked');
if (clicked) {
  // 根据按钮的状态设置样式并禁用按钮
  var father = document.querySelector('.father');
  father.classList.toggle('disabled', clicked === 'true');
  father.disabled = true;
}

// 监听按钮的点击事件
var father = document.querySelector('.father');
father.addEventListener('click', function () {
  // 设置按钮的点击状态为禁用
  this.classList.toggle('disabled', true);
  this.disabled = true;
  // 将按钮的状态存储到本地存储中
  localStorage.setItem('clicked', 'true');
});

通过上述代码,可以实现按钮点击后的禁用状态在刷新后保持不变,并确保用户只能点击一次按钮。



【相关推荐】


  • 你可以参考下这个问题的回答, 看看是否对你有帮助, 链接: https://ask.csdn.net/questions/7636086
  • 除此之外, 这篇博客: js逆向之猿人学-反混淆刷题平台第十题中的 这题应该是刷题平台中js逆向部分最难的一道题目了,而且它跟著名的某数有关系,做完这题后,你也可以说算是与某数有过接触了,本题需要破解的参数其实就是某数当中的MmEwMD,这里先说一下, 本题不会手把手每一步都讲的那么仔细,但是会带大家走一遍抠码的环节,有坑的地方呢就提及一下,至于有些地方为什么这样,又如何解决还是需要各位自己思考的,毕竟做题本身就是为了提高自己,如果什么都说了,都做了,那就失去了做题的意义了,好了话不多说,下面直接开始分析。 部分也许能够解决你的问题。

如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^