Ton_LA 2021-08-30 22:48 采纳率: 100%
浏览 138
已结题

错误提示:Uncaught TypeError: Cannot read property 'style' of undefined at HTMLDocument.myfun1请教各位!

错误提示:Uncaught TypeError: Cannot read property 'style' of undefined at HTMLDocument.myfun1请教各位!

<title>元素跟随鼠标移动</title>
  • 写回答

3条回答 默认 最新

  • 关注

    错误提示:Uncaught TypeError: Cannot read property 'style' of undefined

    这个错误是指 调用这个style属性的对象是 undefined
    比如你的代码是 obj.style
    就是指obj变量的值是 undefined,undefined不是对象,没法调用style属性
    这种情况一般是因为用document.getElementById("id")没有获取到dom对象
    或者也常见于数组下标越界的情况。
    如你的代码是

    for (var i = 0; i <= 6; i++)
        arr[i].style
    

    比如arr数组下标只有0到5元素存放了有style属性的对象。
    当i=6时下标越界, arr[i]的值就是 undefined

    arr[i].style 也就成了 undefined.style

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 9月7日
  • 已采纳回答 8月30日
  • 创建了问题 8月30日