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


为什么焦点元素没有变化?

最佳答案

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

10-04 16:16