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

时间:2015-08-11 18:42:29

标签: javascript

  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

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

1 个答案:

答案 0 :(得分:0)

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