代码报错
解决过程
我想获取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;
}