Cannot read property ‘width‘ of undefined“

问题: 获取元素当前显示的样式
报错: Cannot read property ‘width’ of undefined"
原因: currentStyle只有IE浏览器支持,其他的浏览器都不支持
解决方法:
(1)其他浏览器使用getComputedStyle()获取。
(2)同时兼容:
相关知识:
getComputedStyle()是window的方法,可直接使用
第一个参数:
需要获取样式的元素
第二个参数:
可以传递一个伪元素,一般选择null
返回一个对象,对象中封装了当前元素对应的样式
通过 对象.样式名 读取
若获取的样式没有设置,则会获取到真实的值,而不是默认值。 不支持IE8及以下浏览器;
currentStyle与ComputedStyle获取到的样式都是只读的,只能通过style属性修改

代码:

	<script type="text/javascript">
		
		window.onload = function(){
			var box1 = document.getElementById("box1");
			var btn01 = document.getElementById("btn01");
			btn01.onclick = function(){
				//alert(box1.style.width);	
				/*
				 获取元素当前显示的样式
				 	语法:元素.currentStyle.样式名
				 	
				 	currentStyle只支持IE浏览器,其他的浏览器都不可以
				 * */
				
				//alert(box1.currentStyle.width);
				//alert(box1.currentStyle.backgroundColor);
				
				
				/*
				 在其他浏览器里使用getComputedStyle()
				 	这个是window的方法,可直接使用
				 	第一个参数:
				 		需要获取样式的元素
				 	第二个参数:
				 		可以传递一个伪元素,一般选择null
					
					返回一个对象,对象中封装了当前元素对应的样式
						通过  对象.样式名  读取
						若获取的样式没有设置,则会获取到真实的值,而不是默认值
					
					不支持IE8及以下浏览器
				 
				 
				 * */
				/*var obj = getComputedStyle(box1,null);
				alert(getComputedStyle(box1,null).width);*/
				
				//正常浏览器
				//alert(getComputedStyle(box1,null).backgroundColor);
				
				//IE8
				//alert(box1.currentStyle.backgroundColor);
				
				//alert(getStyle(box1,"width"));
				
				var w = getStyle(box1,"width");
				alert(w);
			};
			
			function getStyle(obj,name){
				if(getComputedStyle){
					//正常浏览器
				return getComputedStyle(obj,null)[name];
				}else{
					//IE8
				return obj.currentStyle[name];
				}
			}
		};
		
	</script>
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据提供的引用内容来看,"Cannot read property VibratorPlayVibration of undefined" 是一个错误消息。然而,根据提供的引用内容中没有相关的信息能够解释这个错误的具体原因或如何处理。为了解决这个问题,我建议你尝试以下几个步骤: 1. 检查代码: 确保你的代码中没有拼写错误或语法错误。尤其是检查你是否正确地引入了相关的库或模块。 2. 查看文档或错误信息: 如果你使用了特定的库或框架,查看它们的官方文档或错误消息以获取更多关于这个错误的信息。这些资源通常提供了关于如何解决常见错误的指导。 3. 搜索互联网: 使用搜索引擎搜索你遇到的错误消息,看看是否有其他人在类似的情况下遇到了相同的问题,并找到了解决方案。 4. 寻求帮助: 如果你无法解决这个问题,你可以向相关的开发社区或论坛寻求帮助。在这些地方,你可以提供更多的细节和代码示例,以便其他开发者可以更好地帮助你。 请记住,由于缺乏具体的信息,我无法给出精确的解决方案。你可能需要进一步调查和调试才能解决这个问题。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Cannot read property map of undefined](https://blog.csdn.net/estherlxy/article/details/106493056)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [Vue打包Cannot read property 'tapPromise' of undefined报错处理.docx](https://download.csdn.net/download/rongbo91/77070400)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [jQuery 出现Cannot read property ‘msie’ of undefined错误的解决方法](https://download.csdn.net/download/weixin_38693586/14805574)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值