Uncaught TypeError: Cannot set property 'display' of undefined解决

本文详细解析了使用JavaScript操作DOM时常见的错误,如使用getElementsByClassName获取元素集合并尝试设置样式导致的TypeError,提供了正确的遍历集合及设置元素样式的示例代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这个问题不是CSS的问题,而是一个纯javascript的问题。

如果大家使用的是JQuery,有$这个符号,可以看看下面这些文字,如果没有,直接跳过即可。

这是看一个大佬的博客,学习到的,这里附上链接:
https://www.cnblogs.com/Renyi-Fan/p/9013126.html

在 prototype 和DWR 的JS 类库里也有这个符号,
在这两个类库里的这个符号是代表 document.getElementById()这个函数。
为了达到document.getElementById() 这个函数的效果,JQuery 也试用了 $() 这个符号。

也就是说:
$("#myId")就是原生JS里面的document.getElementById(“myId”)

类似这样子的改变,还有以下常见方式:

    $("div p"); // (1)

     $("div.container"); // (2)   

     $("div #msg"); // (3)   

     $("table a",context); // (4)
  • 第一行代码得到所有标签下的P元素。

  • 第二行代码得到class 为container的元素。

  • 第三行代码得到标签下面id为msg的元素。

  • 第四行代码得到context为上下文的table里面所有的连接元素。

    jquery就是通过这样的方式来找到Dom对象里面的元素。跟CSS的选择器相类似

下来看看这个问题:

Uncaught TypeError: Cannot set property ‘display’ of undefined

在这里插入图片描述
首先,css写得没错,问题出在Javascript当中的 getElementsByClassName(“xxx”),

这个方法得到的是一个由class="xxx"的所有元素组成的集合,而不是单个元素;

集合是没有display属性的,集合中的元素才有display属性。当你试图做 集合.style.display的时候,自然会报错。

所以你这个问题的解决方案应该是:遍历集合中所有的元素,然后给每个元素都加上display="none"的属性。示例代码如下:

	        var divset=document.getElementsByClassName("el-upload-list");
        	for (var i = 0; i<divset.length;i++) {
        		   divset[i].style.display="block";
        		 };

修改之后成功。


END

good lunck to you

<script lang=&quot;ts&quot; setup> import { ref, onMounted } from 'vue' import Navigater from '@/components/Navigation/Navigater.vue' import {getTaskDetail} from '@/pages/taskDetail/api/index' import type { UploadFile } from 'element-plus' import { Edit,Delete, Download, Plus, ZoomIn} from '@element-plus/icons-vue' import type { FormInstance, FormRules } from 'element-plus' const route = useRoute() // 正确获取当前 route 实例 const id = ref() const taskDetail = ref()// 任务详情 const partyUserInfo = ref() const checked1 = ref(false) const addTxt1 = ref('') const container = ref(null) let scaleValue = 1 const getData = async (id) => { const res = await getTaskDetail({id:id}) taskDetail.value = res.data partyUserInfo.value = res.data.partyUserInfo console.log(partyUserInfo.value.avatar)//输出结果https://cdn.shenyantuling.com/prod/image/head/shutiao1.png } // 图片上传 const dialogImageUrl = ref('') const dialogVisible = ref(false) const disabled = ref(false) const handleRemove = (file: UploadFile) => { console.log(file) } const handlePictureCardPreview = (file: UploadFile) => { dialogImageUrl.value = file.url! dialogVisible.value = true } const handleDownload = (file: UploadFile) => { console.log(file) } // 表单校验 const ruleFormRef = ref<FormInstance>() const ruleForm = reactive({ // .... }) const handleResize = () => { const containerWidth = container.value.offsetWidth const containerHeight = container.value.offsetHeight const windowWidth = window.innerWidth const windowHeight = window.innerHeight // 计算缩放比例,保持内容完整显示 scaleValue = Math.min( windowWidth / containerWidth, windowHeight / containerHeight ) container.value.style.transform = `scale(${scaleValue})` container.value.style.transformOrigin = 'top left' } onMounted(() => { handleResize() window.addEventListener('resize', handleResize) id.value = route.query.id getData(id.value) }) onBeforeUnmount(() => { window.removeEventListener('resize', handleResize) }) </script> <template> <div class=&quot;scale-container&quot; ref=&quot;container&quot;> <div class=&quot;content&quot;> <!-- 顶部 logo 导航栏 --> <Navigater class=&quot;top&quot;></Navigater> <!-- 任务详情 --> <div class=&quot;wrap&quot;> <div class=&quot;topBack&quot;> <div class=&quot;return&quot;> <img src=&quot;@/assets/navigater/return.png&quot; alt=&quot;&quot;> </div> <div class=&quot;title&quot;>任务详情</div> </div> <div class=&quot;taskBox&quot;> <div class=&quot;taskLeft&quot;> <!-- 发布者 --> <div class=&quot;publisherBox&quot;> <div class=&quot;avatar&quot;> <img :src=partyUserInfo.avatar alt=&quot;&quot;> </div> <div class=&quot;info&quot;> <div class=&quot;infoTop&quot;> <div class=&quot;name&quot;>avatar</div> <img src=&quot;@/assets/navigater/task1.png&quot; alt=&quot;&quot;> <img src=&quot;@/assets/navigater/task1.png&quot; alt=&quot;&quot;> </div> <div class=&quot;dic&quot;> The First Onchain Treasury Network for Web 3The First Onchain Treasury Network for Web 3The First Onchain Treasury Network for Web 3 </div> </div> </div> <!-- 任务 --> <div class=&quot;taskDic&quot;> <div class=&quot;taskImg&quot;> <img src=&quot;@/assets/navigater/task1.png&quot; alt=&quot;&quot;> </div> <div class=&quot;taskText&quot;> <div class=&quot;taskTitle&quot;>任务标题:关注微博账号赠送3000积分积分赠送</div> <div class=&quot;botBox&quot;> <div class=&quot;type&quot;>任务类型</div> <div class=&quot;copy&quot;> <img src=&quot;@/assets/navigater/task2.png&quot; alt=&quot;&quot;> </div> </div> </div> </div> <div class=&quot;tip&quot;> <img src=&quot;@/assets/navigater/task1.png&quot; alt=&quot;&quot;> <div class=&quot;tipTxt&quot;>Joinly 将在发放奖励前仔细检查,已提交的任务</div> </div> <div class=&quot;taskMain&quot;> <div class=&quot;mainTop&quot;> <img src=&quot;@/assets/navigater/task2.png&quot; alt=&quot;&quot;> <div class=&quot;mainTxt&quot;>关注 crazy crap在微博的账号,账号名称crazy crap</div> </div> <div class=&quot;main&quot;> <el-form ref=&quot;ruleFormRef&quot; style=&quot;max-width: 600px&quot; :model=&quot;ruleForm&quot; status-icon label-width=&quot;auto&quot; class=&quot;demo-ruleForm&quot; > <!-- :rules=&quot;rules&quot; --> <el-card class=&quot;step&quot;> <div class=&quot;stepTop&quot;> <div class=&quot;stepTit&quot;> <el-checkbox v-model=&quot;checked1&quot; size=&quot;large&quot; /> <div class=&quot;stepTxt&quot;>关闭微博关闭微博关闭微博关闭微博关闭微博关闭微博关闭微博关闭微博关闭微博关闭微博</div> </div> <el-button class=&quot;stepBtn&quot; color=&quot;#715EFF&quot; type=&quot;primary&quot; :icon=&quot;Edit&quot; disabled>保存修改</el-button> </div> <div class=&quot;stepMain&quot;> <div class=&quot;title&quot;>添加图片</div> <el-upload action=&quot;#&quot; list-type=&quot;picture-card&quot; :auto-upload=&quot;false&quot; :limit=3> <el-icon><Plus /></el-icon> <template #file=&quot;{ file }&quot;> <div> <img class=&quot;el-upload-list__item-thumbnail&quot; :src=&quot;file.url&quot; alt=&quot;&quot; /> <span class=&quot;el-upload-list__item-actions&quot;> <span class=&quot;el-upload-list__item-preview&quot; @click=&quot;handlePictureCardPreview(file)&quot; > <el-icon><zoom-in /></el-icon> </span> <span v-if=&quot;!disabled&quot; class=&quot;el-upload-list__item-delete&quot; @click=&quot;handleRemove(file)&quot; > <el-icon><Delete /></el-icon> </span> </span> </div> </template> </el-upload> <el-dialog v-model=&quot;dialogVisible&quot;> <img w-full :src=&quot;dialogImageUrl&quot; alt=&quot;Preview Image&quot; /> </el-dialog> <div class=&quot;title&quot;>添加文字</div> <el-input class=&quot;addTxt&quot; type=&quot;textarea&quot; v-model=&quot;addTxt1&quot; placeholder=&quot;请输入文字&quot; /> </div> </el-card> <el-card class=&quot;step&quot;> <div class=&quot;stepTop&quot;> <div class=&quot;stepTit&quot;> <el-checkbox v-model=&quot;checked1&quot; size=&quot;large&quot; /> <div class=&quot;stepTxt&quot;>关闭微博关闭微博关闭微博关闭微博关闭微博关闭微博关闭微博关闭微博关闭微博关闭微博</div> </div> <el-button class=&quot;stepBtn&quot; color=&quot;#715EFF&quot; type=&quot;primary&quot; :icon=&quot;Edit&quot; disabled>保存修改</el-button> </div> <div class=&quot;stepMain&quot;> <div class=&quot;title&quot;>添加图片</div> <el-upload action=&quot;#&quot; list-type=&quot;picture-card&quot; :auto-upload=&quot;false&quot; :limit=3> <el-icon><Plus /></el-icon> <template #file=&quot;{ file }&quot;> <div> <img class=&quot;el-upload-list__item-thumbnail&quot; :src=&quot;file.url&quot; alt=&quot;&quot; /> <span class=&quot;el-upload-list__item-actions&quot;> <span class=&quot;el-upload-list__item-preview&quot; @click=&quot;handlePictureCardPreview(file)&quot; > <el-icon><zoom-in /></el-icon> </span> <span v-if=&quot;!disabled&quot; class=&quot;el-upload-list__item-delete&quot; @click=&quot;handleRemove(file)&quot; > <el-icon><Delete /></el-icon> </span> </span> </div> </template> </el-upload> <el-dialog v-model=&quot;dialogVisible&quot;> <img w-full :src=&quot;dialogImageUrl&quot; alt=&quot;Preview Image&quot; /> </el-dialog> <div class=&quot;title&quot;>添加文字</div> <el-input class=&quot;addTxt&quot; type=&quot;textarea&quot; v-model=&quot;addTxt1&quot; placeholder=&quot;请输入文字&quot; /> </div> </el-card> <el-card class=&quot;step&quot;> <div class=&quot;stepTop&quot;> <div class=&quot;stepTit&quot;> <el-checkbox v-model=&quot;checked1&quot; size=&quot;large&quot; /> <div class=&quot;stepTxt&quot;>关闭微博关闭微博关闭微博关闭微博关闭微博关闭微博关闭微博关闭微博关闭微博关闭微博</div> </div> <el-button class=&quot;stepBtn&quot; color=&quot;#715EFF&quot; type=&quot;primary&quot; :icon=&quot;Edit&quot; disabled>保存修改</el-button> </div> <div class=&quot;stepMain&quot;> <div class=&quot;title&quot;>添加图片</div> <el-upload action=&quot;#&quot; list-type=&quot;picture-card&quot; :auto-upload=&quot;false&quot; :limit=3> <el-icon><Plus /></el-icon> <template #file=&quot;{ file }&quot;> <div> <img class=&quot;el-upload-list__item-thumbnail&quot; :src=&quot;file.url&quot; alt=&quot;&quot; /> <span class=&quot;el-upload-list__item-actions&quot;> <span class=&quot;el-upload-list__item-preview&quot; @click=&quot;handlePictureCardPreview(file)&quot; > <el-icon><zoom-in /></el-icon> </span> <span v-if=&quot;!disabled&quot; class=&quot;el-upload-list__item-delete&quot; @click=&quot;handleRemove(file)&quot; > <el-icon><Delete /></el-icon> </span> </span> </div> </template> </el-upload> <el-dialog v-model=&quot;dialogVisible&quot;> <img w-full :src=&quot;dialogImageUrl&quot; alt=&quot;Preview Image&quot; /> </el-dialog> <div class=&quot;title&quot;>添加文字</div> <el-input class=&quot;addTxt&quot; type=&quot;textarea&quot; v-model=&quot;addTxt1&quot; placeholder=&quot;请输入文字&quot; /> </div> </el-card> </el-form> </div> </div> </div> <div class=&quot;taskRight&quot;> </div> </div> </div> </div> </div> </template> <style scoped lang=&quot;scss&quot;> .scale-container { width: 1920px; /* 设计稿宽度 */ position: absolute; display: block; top: 0; left: 0; .content { width: 100%; height: 100%; background-color: #000; display: block; .wrap{ width: 1552px; margin: 0 auto; .topBack{ display: flex; width: 1552px; height: 74px; justify-content: left; align-items: center; border-bottom: 1px solid #444; margin-bottom: 31px; .return{ width: 24px; height: 24px; margin-right: 10px; img{ width: 100%; height: 100%; } } .title{ font-family: PingFang SC; font-weight: 600; font-style: Semibold; font-size: 20px; color:#fff; line-height: 100%; letter-spacing: -0.3px; } } .taskBox{ display: flex; width: 1174px; margin: 0 auto; .taskLeft{ width: 787px; .publisherBox{ width: 787px; height:100px; display: flex; background-color: #232323; border-radius: 12px; padding: 16px; margin-bottom: 30px; .avatar{ width: 48px; height: 48px; margin-right: 16px; img{ width: 100%; height: 100%; border-radius: 50%; } } .info{ .infoTop{ display: flex; justify-content: left; align-items: center; .name{ font-family: PingFang SC; font-weight: 600; font-style: Semibold; font-size: 20px; color:#fff; line-height: 100%; letter-spacing: -0.3px; } img{ width: 16px; height: 16px; border-radius: 50%; margin-left: 8px; } } .dic{ width: 536px; margin-top: 14px; font-family: PingFang SC; font-weight: 400; font-style: Regular; font-size: 14px; line-height: 21px; letter-spacing: -0.3px; color:#A6A6A6; display: -webkit-box; /* 关键属性:弹性伸缩盒子 */ -webkit-box-orient: vertical; /* 排列方向:垂直 */ -webkit-line-clamp: 2; /* 限制行数:2行 */ overflow: hidden; /* 溢出隐藏 */ text-overflow: ellipsis; /* 溢出显示省略号 */ word-break: break-word; /* 长单词换行处理 */ } } } .taskDic{ width: 786px; height: 150px; top: 346px; left: 373px; display: flex; padding-top: 32px; border-top: 1px solid #444; .taskImg{ img{ width: 160px; height: 120PX; border-radius: 12px; } } .taskText{ color: #fff; margin-left: 23px; .taskTitle{ width: 546px; height: 80px; font-family: PingFang SC; font-weight: 600; font-style: Semibold; font-size: 26px; letter-spacing: -0.3px; // display: -webkit-box; /* 关键属性:弹性伸缩盒子 */ // -webkit-box-orient: vertical; /* 排列方向:垂直 */ // -webkit-line-clamp: 2; /* 限制行数:2行 */ // overflow: hidden; /* 溢出隐藏 */ // text-overflow: ellipsis; /* 溢出显示省略号 */ // word-break: break-word; /* 长单词换行处理 */ } .botBox{ display: flex; justify-content: space-between; align-items: center; width: 603px; .type{ width: 64px; height: 30px; text-align: center; font-family: PingFang SC; font-weight: 600; font-style: Semibold; font-size: 12px; line-height: 30px; letter-spacing: -0.3px; border-radius: 4px; background-color: #232323; } .copy{ width: 30px; height: 30px; img{ width: 100%; height: 100%; } } } } } .tip{ display: flex; justify-content: left; align-items: center; margin: 32px auto; img{ width: 16px; height: 16px; border-radius: 50%; margin-right: 10px; } .tipTxt{ color:#fff;font-family: PingFang SC; font-weight: 400; font-style: Regular; font-size: 14px; line-height: 100%; letter-spacing: -0.3px; } } .taskMain{ width: 787px; padding: 24px; border-radius: 12px; background-color: #232323; .mainTop{ display: flex; justify-content: left; align-items: center; margin-bottom: 23px; img{ width: 24px; height: 24px; border-radius: 4px; margin-right: 10px; } .mainTxt{ font-family: PingFang SC; font-weight: 600; font-style: Semibold; font-size: 18px; color:#fff; line-height: 100%; letter-spacing: -0.3px; } } .main{ .step{ width: 739px; background-color: #121212; border: 1px solid #121212; .stepTop{ width: 715px; display: flex; justify-content: space-between; align-items: center; border-bottom: 1px solid #444; .stepTit{ display: flex; justify-content: space-between; align-items: center; .stepTxt{ font-family: PingFang SC; font-weight: 400; font-style: Regular; font-size: 14px; color:#A6A6A6; line-height: 100%; letter-spacing: -0.3px; margin-left: 10px; } } .stepBtn{ margin-right: 10px; margin-bottom: 3px; } } .stepMain{ width: 667px; margin: 0 auto; .title{ font-family: PingFang SC; font-weight: 400; font-style: Regular; font-size: 12px; color:#fff; letter-spacing: -0.3px; margin: 10px 0; } .addTxt{ &:deep(.el-input){ display: block; width: 667px; height: 80px !important; background-color: #363636 !important; border: none; border-radius: 8px; } } } } } } } .taskRight{ width: 339px; margin-left: 48px; } } } } } </style> 中console.log(partyUserInfo.value.avatar)的输出结果为https://cdn.shenyantuling.com/prod/image/head/shutiao1.png。 但是 报错显示:taskDetail.vue:98 Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'avatar') at Proxy._sfc_render (taskDetail.vue:98:53) 即<img :src=partyUserInfo.avatar alt=&quot;&quot;>中找不到avatar
最新发布
07-29
### Vue2 打包后 Uncaught TypeError: Cannot read property 'call' of undefined解决方案 在 Vue2 项目中,打包后出现 `Uncaught TypeError: Cannot read property 'call' of undefined` 错误通常与 Webpack 配置或第三方库的解析问题有关。以下是详细的分析和解决方案: #### 1. **Webpack 配置中的 noParse** 如果项目中使用了某些大型的第三方库(例如 `ali-oss`),这些库可能包含预编译的 JavaScript 文件,而 Webpack 在默认情况下会尝试解析这些文件。这可能导致错误的发生。解决方法是在 Webpack 配置中添加 `noParse`,以忽略对特定库的解析。 在 `webpack.base.conf.js` 文件中修改配置如下: ```javascript module.exports = { module: { rules: [ // 其他规则... ] }, // 添加 noParse 配置 noParse: [/ali-oss/] }; ``` 通过上述配置,可以避免 Webpack 对 `ali-oss` 库的不必要解析[^1]。 #### 2. **检查变量初始化** 错误也可能由未正确初始化的变量引起。确保所有需要访问的对象属性都已正确定义。例如,以下代码会导致类似错误: ```javascript let obj = {}; console.log(obj.call); // Uncaught TypeError: Cannot read property 'call' of undefined ``` 可以通过以下方式避免此类问题: ```javascript let obj = { call: () => console.log('Called') }; if (obj && typeof obj.call === 'function') { obj.call(); } ``` #### 3. **异步数据加载问题** 如果项目中存在异步数据加载逻辑,确保在数据完全加载后再访问相关属性。例如,在 Vuex 或 Axios 请求中,确保数据已成功返回后再进行操作。 示例代码: ```javascript async function fetchData() { const response = await axios.get('/api/data'); if (response.data) { console.log(response.data.call); } else { console.error('Data is undefined'); } } ``` #### 4. **Vue 插件或依赖版本冲突** 如果错误发生在插件注册阶段(如 `Vue.use()`),可能是由于 Vue 插件或依赖版本不兼容导致的。例如,`vue-router` 版本过高可能导致与 Vue2 不兼容的问题。可以通过降级插件版本来解决。 安装兼容的 `vue-router` 版本: ```bash npm install vue-router@3.5.3 # 或者 yarn add vue-router@3.5.3 ``` 确保项目中使用的插件版本与 Vue2 兼容[^4]。 #### 5. **调试与日志记录** 为了更精确地定位问题,可以在报错位置添加调试信息。例如: ```javascript try { Vue.use(SomePlugin); } catch (error) { console.error('Error in plugin registration:', error); } ``` --- ### 示例代码调整 假设问题出现在插件注册阶段,可以参考以下代码调整: ```javascript // main.js import Vue from 'vue'; import App from './App.vue'; import router from './router'; // 确保插件已正确导入 import SomePlugin from 'some-plugin'; Vue.config.productionTip = false; try { Vue.use(SomePlugin); } catch (error) { console.error('Failed to register plugin:', error); } new Vue({ router, render: h => h(App), }).$mount('#app'); ``` --- ### 总结 通过调整 Webpack 配置、检查变量初始化、处理异步数据加载问题以及确保插件版本兼容性,可以有效解决 Vue2 打包后出现的 `Uncaught TypeError: Cannot read property 'call' of undefined` 错误[^1][^2][^3][^4]。 ---
评论 14
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值