如何使最后一个焦点文件应该在javascript中刷新html页面后进行焦点处理

how to make last focused filed should be focused after refresh html page in javascript

本文关键字:焦点 html 处理 刷新 javascript 文件 最后一个 何使      更新时间:2023-09-26

刷新页面后,我正在尝试将最后一个编辑的(聚焦)元素聚焦为聚焦。这是我的代码:

<body>
<input id="first"><br>
<input id="second"><br>
<input id="third"><br>
<input id="four"><br>
<input id="five"><br>
</body>

以下是使用localStorageonfocus事件的解决方案:

<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中进行了测试。