js报错之item.getAttribute is not a function

代码报错
在这里插入图片描述
解决过程
我想获取item下的属性changePrice,报错
item.getAttribute is not a function,
我就用console.log(item.getAttribute(‘changePrice’));想打印出来这个结果,但是为空,价格非法
在这里插入图片描述

我就去打印上一步的item,一步一步的向上找,最后发现自己的dd容器里面没有这个属性,我只是将js文件里面的数据引用过来而已,没有设置这个changePrice
建议大家如果报错,先查找自己这个里面是否设置这个属性
代码附上

ddNode.setAttribute('changePrice',item.changePrice);
function priceSum(arr){
		// 获取原始价格
		// console.log(arr);
		var price = goodData.goodsDetail.price;
		// 获取价格容器
		var priceNode = document.querySelector('.mainCon .chooseWrap .priceWrap .priceTop .price em');
		arr.forEach(function(item){
			// 在这个循环中 item对应的是 传入数组当中存储的每一个dd元素
			// console.log(item);
			// console.log(item.getAttribute('changePrice'));
			if(item){
				// console.log(item.changePrice);
				price += parseInt(item.getAttribute('changePrice'));
			}
			console.log(price);
		})
		priceNode.innerHTML = price;
	}

版权声明:本文为weixin_45866961原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。