Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/385.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么我的输入在切换时没有显示为activeElement?_Javascript_Html - Fatal编程技术网

Javascript 为什么我的输入在切换时没有显示为activeElement?

Javascript 为什么我的输入在切换时没有显示为activeElement?,javascript,html,Javascript,Html,当我通过元素进行制表时,我的输入不会显示在控制台中,但当我从按钮中取出制表符时,按钮会显示,为什么输入不会显示在控制台中 (函数(){ let element=document.getElementById('MyDiv'); 让focusables=element.querySelectorAll('input[type=“text”],button:not([disabled]); //console.log(焦点); 设firstFocusableElement=focusables[0

当我通过元素进行制表时,我的输入不会显示在控制台中,但当我从按钮中取出制表符时,按钮会显示,为什么输入不会显示在控制台中

(函数(){
let element=document.getElementById('MyDiv');
让focusables=element.querySelectorAll('input[type=“text”],button:not([disabled]);
//console.log(焦点);
设firstFocusableElement=focusables[0];
设lastFocusableElement=focusables[focusables.length-1];
//console.log(firstFocusableElement);
//log(lastFocusableElement);
元素。addEventListener('keydown',函数(e){
如果(e.key==='Tab'){
if(document.activeElement==lastFocusableElement){
console.log(document.activeElement);
firstFocusableElement.focus();
e、 预防默认值();
}
}
});
})();
#MyContainer{
背景色:浅绿色;
高度:100vh;
宽度:100%;
}
#MyDiv{
显示:块;
宽度:500px;
左:50%;
左边距:-250px;
背景色:仿古白色;
位置:固定;
边框:1px实心;
高度:500px;
}
#包装纸{
边缘顶部:80px;
}

姓名:

中名:
姓氏:
点击我!
两个原因:

  • 当您获得
    聚焦对象时,您使用
    输入[type=“text”]
    作为文本输入的选择器,但在html中,您的输入没有
    类型属性为
    文本,因此它们都不匹配
  • 在登录之前,请检查:
    if(document.activeElement==lastFocusableElement)
    ,这意味着它将仅为您的最后一个元素(按钮)触发