更多操作
Xp00000000(留言 | 贡献) 无编辑摘要 |
Xp00000000(留言 | 贡献) 小无编辑摘要 标签:已被回退 |
||
第1行: | 第1行: | ||
<html> | <html> | ||
< | <script> | ||
. | function cycleText(element) { | ||
const states = ["状态A", "状态B", "状态C"]; // 定义所有状态 | |||
const currentText = element.textContent.trim(); | |||
// 获取当前状态的索引 | |||
let currentIndex = states.indexOf(currentText); | |||
if(currentIndex === -1) currentIndex = 0; | |||
// 计算下一个状态索引(循环) | |||
const nextIndex = (currentIndex + 1) % states.length; | |||
// 更新文本 | |||
element.textContent = states[nextIndex]; | |||
// 可选:存储当前状态(需要唯一ID) | |||
if(element.id) { | |||
localStorage.setItem('textState_' + element.id, nextIndex); | |||
} | |||
} | } | ||
// 初始化恢复状态 | |||
document.addEventListener('DOMContentLoaded', function() { | |||
const element = document.getElementById('cycleDemo'); | |||
if(element && element.id) { | |||
const savedIndex = localStorage.getItem('textState_' + element.id); | |||
if(savedIndex !== null) { | |||
const states = ["状态A", "状态B", "状态C"]; | |||
element.textContent = states[parseInt(savedIndex)]; | |||
} | } | ||
}; | } | ||
}); | |||
</script> | </script> | ||
<div id=" | |||
<div id="cycleDemo" onclick="cycleText(this)" | |||
style="cursor:pointer; padding:10px; background:#f0f0f0; display:inline-block;"> | |||
状态A | |||
</div> | |||
</html> | </html> |
2025年7月1日 (二) 21:08的版本
状态A