更多操作
Xp00000000(留言 | 贡献) 小无编辑摘要 |
Xp00000000(留言 | 贡献) 小无编辑摘要 |
||
第1行: | 第1行: | ||
{ | <html> | ||
<style> | |||
.normal-style { | |||
padding: 10px; | |||
background: #eee; | |||
cursor: pointer; | |||
margin: 10px 0; | |||
border-radius: 4px; | |||
transition: background 0.3s; | |||
} | |||
.clicked-style { | |||
background: #ccc; | |||
box-shadow: inset 0 0 5px rgba(0,0,0,0.3); | |||
} | |||
</style> | |||
<script> | |||
function toggleColor(element) { | |||
element.classList.toggle("clicked-style"); | |||
// 更新文本内容 | |||
element.textContent = element.classList.contains("clicked-style") | |||
? "已选中 (点击取消)" | |||
: "点击选择"; | |||
// 存储状态 | |||
localStorage.setItem('boxState', element.classList.contains("clicked-style")); | |||
} | |||
// 页面加载时恢复状态和文本 | |||
window.onload = function() { | |||
var box = document.getElementById('myInteractiveBox'); | |||
if(box) { | |||
var isClicked = localStorage.getItem('boxState') === 'true'; | |||
box.classList.toggle("clicked-style", isClicked); | |||
box.textContent = isClicked | |||
? "已选中 (点击取消)" | |||
: "点击选择"; | |||
} | |||
}; | |||
</script> | |||
<div id="myInteractiveBox" class="normal-style" onclick="toggleColor(this)"> | |||
点击选择 | |||
</div> | |||
</html> |
2025年7月1日 (二) 20:44的版本
点击选择