切换switch 保持上次状态,即使在重新加载时也是如此

我使用的是W3School (this one)的拨动switch .在W3School编辑器中,它在重新加载时返回到已建立的"选中"或"未选中"位置,但无论出于什么原因-如果我"选中"我的,然后重新加载,它将保持选中状态,或者如果我取消选中然后重新加载,它将保持未选中状态.

对于我正在做的项目来说,重要的是在初始加载/任何重新加载/重新访问时取消选中它.

我很好奇这是不是有缓存问题,因为我是在我的电脑上本地处理它(编辑html的写字板,查看文件的Firefox),但我将我的工作上传到了NeoCities,问题仍然存在,所以我认为这意味着这是代码本身.

<head>
<style>
.selector {
background-color:rgba(255, 255, 0, 0.3);
text-align:center;
border:1px solid gold;
width: 50vw;
max-width:500px;
border-radius:2em;
padding:1em;
font-size:20px;
color:gold;
margin:2em auto auto;
}

.switch {
  position: relative;
  display: inline-block;
  width: 50px;
  height: 24px;
}

.switch input { 
  opacity: 0;
  width: 0;
  height: 0;
}

.slider {
  position: absolute;
  cursor: pointer;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: darkblue;
  -webkit-transition: .4s;
  transition: .4s;
  border-radius: 34px;
}

.slider:before {
  position: absolute;
  content: "";
  height: 16px;
  width: 16px;
  left: 4px;
  bottom: 4px;
  background-color: gold;
  -webkit-transition: .4s;
  transition: .4s;
  border-radius: 50%;
}

input:checked + .slider {
  background-color: cornflowerblue;
}


input:checked + .slider:before {
  -webkit-transform: translateX(26px);
  -ms-transform: translateX(26px);
  transform: translateX(26px);
}
</style>
</head>
<body>

<div class="selector">Unchecked 
  <label class="switch">
    <input type="checkbox" onclick="hoverFunction()">
     <span class="slider" alt="toggle switch button"></span>
  </label>
 Checked</div>
</body>

我怎么才能解决这个问题呢?

推荐答案

这是由Firefox引起的.如果您添加了标志AutoComplete="off",它应该可以防止这种行为,并且您可以使用"选中"或"取消选中"标志来设置默认状态.

例如:

<input type="checkbox" onclick="hoverFunction()" autocomplete="off" unchecked>

或者:

<input type="checkbox" onclick="hoverFunction()" autocomplete="off" checked>

Html相关问答推荐

z—index总是 destruct 我的头,我该如何修复它?

Font Awesome 6插入符号未显示

如何删除在小屏幕上单击按钮时出现的蓝色方块?

如何让QML`Text`元素内的链接在悬停时显示鼠标指针?

<;img>;和具有负边距的元素的堆叠顺序

通过POST方法提交html表单时CSV文件无法通过(在使用Django的上下文中)

如何防止可见的滑动抽屉,同时转移HTML方向?

如何在一张表中逃脱边境坍塌--崩溃?

具有可展开行的棱角material 表(垫子表),折叠时行之间仍有间隙

当溢出到滚动条中时, bootstrap 按钮样式会更改

如何从多个TO中获取一个范围标记以溢出其父标记

高度:适合-内容不拉伸以适合内部长度

如何实现与内嵌图像对齐的自动换行?

调整窗口大小时文本重叠导航栏

将 HTML 元素粘贴到容器底部,而不剪裁其顶部

如何为某些行具有 rowspan 的表的每个奇数行着色

在身体外部创建 tanget 45° div

在 SQL 中合并 HTML 表中的单元格

如何使用 CSS 制作蜂窝状网格?

如何使用 css 将图像变成黑色并使文本出现在悬停时?