为什么focus()不能用于div元素?

问题描述 投票:1回答:1
  var targetElement = document.getElementsByClassName('hello-world-div')[0];
  var focusElement = document.activeElement;
  console.log("currently focused on: ");
  console.log(focusElement); //this shows the body element  

  targetElement.focus();
  focusElement = document.activeElement;
  console.log("forced focus on: ");
  console.log(focusElement); // this still shows the body element

为什么焦点元素不会改变?

javascript
1个回答
0
投票

我必须将tabIndex属性添加到“hello-world-div”元素,以便focus()和blur()可以工作!

© www.soinside.com 2019 - 2024. All rights reserved.