前言

首先看代码:

        let colors = ['green', 'yellow', 'black', 'blue', 'pink', 'yellowgreen']let random = Math.floor(Math.random() * colors.length)const div = document.querySelectorAll('div')div.style.backgroundColor = colors[random]

这个代码很简单,其实就是通过随机数来改变div的颜色,达到每次刷新颜色都是随机的目的。

但是,这个只是学习的而已,为了加强对内置对象Math的方法使用而做的。

那么还没有学到API的小聪明就会,好奇,为什么这个代码会在浏览器报错,并出现下面的问题?


分析

其实解决方法还是蛮简单的,首先我们得了解document.querySelectorAll方法的意义,我们可以在浏览器输入MDN,打开第一个连接,如下图:

这个时候,我们把document.querySelectorAll方法输入到搜索框,进行查找:

然后我们查看返回值:

可以发现,document.querySelectorAll返回的是一个list,但是我们的代码中最后一句,右边是一个值,这里就出现了错误。

所以这个时候,我们就得考虑把document.querySelectorAll更改为document.querySelector。我们先去MDN查看document.querySelector的返回值:

非常符合一对一原则,所以我们删去All,运行正确:

最后

最后代码:

        let colors = ['green', 'yellow', 'black', 'blue', 'pink', 'yellowgreen']let random = Math.floor(Math.random() * colors.length)// 注意不要写出document.querySelectorAll,这样会报错!!const div = document.querySelector('div')div.style.backgroundColor = colors[random]

Uncaught TypeError: Cannot set properties of undefined (setting ‘backgroundColor‘)的解决方法相关推荐

  1. js代码中for循环里绑定onclick事件报错Uncaught TypeError Cannot set properties of undefined (setting ‘className‘)

    jsfor循环里绑定onclick事件报错Uncaught TypeError Cannot set properties of undefined (setting 'className') 我想要 ...

  2. 【vue】文件批量上传报错问题解决Uncaught TypeError: Cannot set properties of undefined (setting ‘status‘)

    一.问题描述 最近在vue项目上,使用el-upload组件实现多文件上传功能时出现了如下的报错内容. Uncaught TypeError: Cannot set properties of und ...

  3. Uncaught TypeError: Cannot set properties of undefined (setting ‘ innerHTML ) 问题

    项目考核已经结束,我想分享一下我这次写项目的收获. 首先是"> Uncaught TypeError: Cannot set properties of undefined (sett ...

  4. TypeError: Cannot set properties of undefined (setting ‘innerHTML‘)

    Vue调用本地组件定义的变量,报错TypeError: Cannot set properties of undefined (setting 'innerHTML'),可能是回调函数内无法直接访问本 ...

  5. 报错,Uncaught TypeError: Cannot read properties of undefined (reading ‘inputValue‘) at <anonymous>

    错误: Uncaught TypeError: Cannot read properties of undefined (reading 'inputValue')     at <anonym ...

  6. Uncaught TypeError: Cannot read properties of undefined (reading ‘_modulesNamespaceMap‘)

    [Vue warn]: Property "$store" was accessed during render but is not defined on instance. a ...

  7. TypeError: Cannot set properties of undefined (setting ‘xx‘)

    TypeError: Cannot set properties of undefined (setting 'xx') 目录 TypeError: Cannot set properties of ...

  8. Uncaught TypeError: Cannot read properties of undefined (reading ‘push’) ---- vue-router报错

    坑点 在使用vue-router@4的时候,由于vue3中没有this.router这个写法,所以我就将解构router方法,放在了函数中,由于函数有作用域就报了这个错误:Uncaught TypeE ...

  9. Vue TypeError: Cannot set properties of undefined (setting ‘msg‘) at eval (App.vue?91a0:25:1)

    Vue TypeError: Cannot set properties of undefined (setting 'msg') at eval (App.vue?91a0:25:1) 这个问题是是 ...

最新文章

  1. 手握173篇论文的学术新星被指造假!后续:博士论文被召回
  2. Javascript简介
  3. Projection with Type Hint用法
  4. epic转移游戏_Epic游戏商城更改退款政策 和steam一模一样
  5. 博客更新内容简单介绍
  6. BCB 连接数据库和查询数据
  7. linux lddbus设备,Linux设备驱动程序学习(14)-Linux设备模型(各环节的整合)
  8. Adapter 如果客户需要使用某个类的服务,而这项服务是这个类用一个不同的接口提供的,那么,可以使用适配器模式为客户提供一个期望的接口...
  9. riot.js教程【二】组件撰写准则、预处理器、标签样式和装配方法
  10. 前端开发人员MAC装机工具
  11. SWMM[Storm Water Management Model]模型代码编译调试环境设置
  12. web小白,实战操作拿到网站后台账户和密码
  13. 查询linux服务器有哪些IP在连接
  14. Scala入门系列(7)-Scala之函数式编程
  15. 带加权的贝叶斯自举法 Weighted Bayesian Bootstrap
  16. mysqld_safe启动mysql数据库的参数
  17. Mac启动磁盘已满怎么办?Mac启动磁盘已满怎么清理?
  18. 百亿节点、毫秒级延迟,携程金融基于 NebulaGraph 的大规模图应用实践
  19. 使用 Sublime、WebStorm 开发 Jade
  20. oracle2 gta5,GTA5川崎Ninja H2/H2R MOD_侠盗猎车手5川崎Ninja H2/H2R MOD

热门文章

  1. pytorch + visdom 应用神经网络、CNN 处理手写字体分类
  2. 直播美颜SDK有哪些应用领域?
  3. python扫描文件
  4. 技术文档之起航--QoE有关
  5. 同方国芯拟逾60亿投集成电路产业链公司
  6. 使命召唤4战争名言录——战争与和平
  7. Web前端第四季(jQuery):四:301-jQuery基本过滤器(奇数和偶数)+302-实现隔行换色+401-祖先选择器和子代选择器
  8. ubuntu18.04安装mongoDB 4.0
  9. Anaconda详细安装教程(从头到尾一条龙服务,无需彷徨,图文详解!具详细!)
  10. java实现克莱姆_关于cocos2dx 3.x VideoPlayer的问题