首页 > 技术文章 > [BUG] Uncaught TypeError: Illegal invocation at HTMLDivElement.

whosmeya 2020-02-17 14:03 原文

现象

<div id="clickMe" key="1">点我</div>
clickMe.onclick = function (event) {
  var getAttribute = event.target.getAttribute;
  console.log(getAttribute('key'))
}
// TypeError: Illegal invocation at HTMLDivElement.clickMe.onclick

clickMe.addEventListener('click', function (event) {
  var getAttribute = event.target.getAttribute;
  console.log(getAttribute('key'))
})
// Uncaught TypeError: Illegal invocation at HTMLDivElement.

原因

getAttribute 方法不能赋值给变量.


推荐阅读