在同一脚本中,我有一个非常相似的函数,该脚本使用完全相同的语法,并且运行良好。唯一的区别是,它不使用.blur,并且不在函数内部引用。我只需要创建一个函数,因为.blur不能单独工作。
HTML 5,最新版本的firefox,UTF-8编码
将框更改为窗口。框不起作用。
仅包含相关代码。
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</meta>
</head>
<body>
<input id="box1" type="text" size="15">
<input id="box2" type="text" size="15">
<input id="box3" type="text" size="15">
<input id="box4" type="text" size="15">
<input id="box5" type="text" size="15">
<input id="box6" type="text" size="15">
<input id="box7" type="text" size="15">
<input id="box8" type="text" size="15">
<input id="box9" type="text" size="15">
<input id="box10" type="text" size="15">
<script type="text/javascript">
function blurElement(id) {
document.getElementById(id).blur();
}
const boxes = ["box1", "box2", "box3", "box4", "box5", "box6", "box7", "box8", "box9", "box10"];
for (box = 0; box <= 9; box+=1){
document.getElementById(boxes[box]).onkeypress = function(event) {
//...this works...
}
}
onkeyup = function(event) {
if (event.key === "Enter") {
document.getElementById("submit").click();
//this also works
}
}
for (box = 1; box <= 8; box+=1) {
document.getElementById(boxes[box]).onkeyup = function(event) {
if (event.key === "Tab") {
event.preventDefault();
blurElement(boxes[box]);
//this does not work
}
}
}
</script>
</body>
```HTML
Expected: If the tab key is released whilst a textbox is focused, that textbox loses its focus.
Result: document.getElementById(id).blur(); is null