Uncaught TypeError: Cannot set properties of undefined (setting ‘backgroundColor‘)的解决方法
前言
首先看代码:
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的小聪明就会,好奇,为什么这个代码会在浏览器报错,并出现下面的问题?
![](/assets/blank.gif)
分析
其实解决方法还是蛮简单的,首先我们得了解document.querySelectorAll方法的意义,我们可以在浏览器输入MDN,打开第一个连接,如下图:
![](/assets/blank.gif)
这个时候,我们把document.querySelectorAll方法输入到搜索框,进行查找:
![](/assets/blank.gif)
然后我们查看返回值:
![](/assets/blank.gif)
可以发现,document.querySelectorAll返回的是一个list,但是我们的代码中最后一句,右边是一个值,这里就出现了错误。
所以这个时候,我们就得考虑把document.querySelectorAll更改为document.querySelector。我们先去MDN查看document.querySelector的返回值:
![](/assets/blank.gif)
非常符合一对一原则,所以我们删去All,运行正确:
![](/assets/blank.gif)
最后
最后代码:
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‘)的解决方法相关推荐
- js代码中for循环里绑定onclick事件报错Uncaught TypeError Cannot set properties of undefined (setting ‘className‘)
jsfor循环里绑定onclick事件报错Uncaught TypeError Cannot set properties of undefined (setting 'className') 我想要 ...
- 【vue】文件批量上传报错问题解决Uncaught TypeError: Cannot set properties of undefined (setting ‘status‘)
一.问题描述 最近在vue项目上,使用el-upload组件实现多文件上传功能时出现了如下的报错内容. Uncaught TypeError: Cannot set properties of und ...
- Uncaught TypeError: Cannot set properties of undefined (setting ‘ innerHTML ) 问题
项目考核已经结束,我想分享一下我这次写项目的收获. 首先是"> Uncaught TypeError: Cannot set properties of undefined (sett ...
- TypeError: Cannot set properties of undefined (setting ‘innerHTML‘)
Vue调用本地组件定义的变量,报错TypeError: Cannot set properties of undefined (setting 'innerHTML'),可能是回调函数内无法直接访问本 ...
- 报错,Uncaught TypeError: Cannot read properties of undefined (reading ‘inputValue‘) at <anonymous>
错误: Uncaught TypeError: Cannot read properties of undefined (reading 'inputValue') at <anonym ...
- Uncaught TypeError: Cannot read properties of undefined (reading ‘_modulesNamespaceMap‘)
[Vue warn]: Property "$store" was accessed during render but is not defined on instance. a ...
- TypeError: Cannot set properties of undefined (setting ‘xx‘)
TypeError: Cannot set properties of undefined (setting 'xx') 目录 TypeError: Cannot set properties of ...
- Uncaught TypeError: Cannot read properties of undefined (reading ‘push’) ---- vue-router报错
坑点 在使用vue-router@4的时候,由于vue3中没有this.router这个写法,所以我就将解构router方法,放在了函数中,由于函数有作用域就报了这个错误:Uncaught TypeE ...
- 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) 这个问题是是 ...
最新文章
- 手握173篇论文的学术新星被指造假!后续:博士论文被召回
- Javascript简介
- Projection with Type Hint用法
- epic转移游戏_Epic游戏商城更改退款政策 和steam一模一样
- 博客更新内容简单介绍
- BCB 连接数据库和查询数据
- linux lddbus设备,Linux设备驱动程序学习(14)-Linux设备模型(各环节的整合)
- Adapter 如果客户需要使用某个类的服务,而这项服务是这个类用一个不同的接口提供的,那么,可以使用适配器模式为客户提供一个期望的接口...
- riot.js教程【二】组件撰写准则、预处理器、标签样式和装配方法
- 前端开发人员MAC装机工具
- SWMM[Storm Water Management Model]模型代码编译调试环境设置
- web小白,实战操作拿到网站后台账户和密码
- 查询linux服务器有哪些IP在连接
- Scala入门系列(7)-Scala之函数式编程
- 带加权的贝叶斯自举法 Weighted Bayesian Bootstrap
- mysqld_safe启动mysql数据库的参数
- Mac启动磁盘已满怎么办?Mac启动磁盘已满怎么清理?
- 百亿节点、毫秒级延迟,携程金融基于 NebulaGraph 的大规模图应用实践
- 使用 Sublime、WebStorm 开发 Jade
- oracle2 gta5,GTA5川崎Ninja H2/H2R MOD_侠盗猎车手5川崎Ninja H2/H2R MOD
热门文章
- pytorch + visdom 应用神经网络、CNN 处理手写字体分类
- 直播美颜SDK有哪些应用领域?
- python扫描文件
- 技术文档之起航--QoE有关
- 同方国芯拟逾60亿投集成电路产业链公司
- 使命召唤4战争名言录——战争与和平
- Web前端第四季(jQuery):四:301-jQuery基本过滤器(奇数和偶数)+302-实现隔行换色+401-祖先选择器和子代选择器
- ubuntu18.04安装mongoDB 4.0
- Anaconda详细安装教程(从头到尾一条龙服务,无需彷徨,图文详解!具详细!)
- java实现克莱姆_关于cocos2dx 3.x VideoPlayer的问题