刷新页面后,我正在尝试将最后一个编辑的(聚焦)元素聚焦为聚焦。这是我的代码:
<body>
<input id="first"><br>
<input id="second"><br>
<input id="third"><br>
<input id="four"><br>
<input id="five"><br>
</body>
以下是使用localStorage
和onfocus
事件的解决方案:
<body>
<script>
window.addEventListener("load", function () {
var active = localStorage.getItem("myTestApp");
if (active) {
e = document.getElementById(active).focus();
console.log('Focused to ' + active);
}
});
</script>
<input id="first" onfocus="localStorage.setItem('myTestApp','first')"/><br>
<input id="second" onfocus="localStorage.setItem('myTestApp','second')"/><br>
<input id="third" onfocus="localStorage.setItem('myTestApp','third')"/><br>
<input id="four" onfocus="localStorage.setItem('myTestApp','four')"/><br>
<input id="five" onfocus="localStorage.setItem('myTestApp','five')"/><br>
</body>
它已经在Chrome、Firefox和IE中进行了测试。