我试图在刷新页面后将最后编辑的(聚焦)元素聚焦为焦点。这是我的代码:
<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 中进行了测试。
关于javascript - 如何在javascript中刷新html页面后使最后一个聚焦的字段应该聚焦,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33695906/