【发布时间】:2021-09-14 10:05:49
【问题描述】:
var main = document.querySelector("#main");//SELECTION OF id//
//Array of items//
var arr = [
{cartnumber:"0", img:"https://images.unsplash.com/photo-1596552183299-000ef779e88d?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=400&q=80",name:"Microwave",category:"electronics",price:"$300"},
{cartnumber:"0", img:"https://images.unsplash.com/photo-1588854337115-1c67d9247e4d?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=750&q=80",name:"Refrigerator",category:"electronics",price:"$500"},
{cartnumber:"0", img:"https://images.unsplash.com/photo-1461151304267-38535e780c79?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=890&q=80",name:"Television",category:"electronics",price:"$600"},
{cartnumber:"0", img:"https://images.unsplash.com/photo-1619017098958-57b1e2d275e4?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=375&q=80",name:"Mobile",category:"electronics",price:"$1000"},
{cartnumber:"0", img:"https://images.unsplash.com/photo-1525825691042-e14d9042fc70?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=753&q=80",name:"Earpodes",category:"electronics",price:"$100"},
{cartnumber:"0", img:"https://images.unsplash.com/photo-1496181133206-80ce9b88a853?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=751&q=80",name:"Laptop",category:"electronics",price:"$2000"},
{cartnumber:"0", img:"https://images.unsplash.com/photo-1611186871348-b1ce696e52c9?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=750&q=80",name:"Macbook",category:"electronics",price:"$10000"}
]
//显示产品列表//
函数显示(){
变种温度 = `;
arr.forEach(function (elem,index) {
温度+= <div class="card"> <div id="circle"> <img src="${elem.img}"> </div> <div id="txt"> <h4>${elem.name}</h4> <h6>${elem.category}</h6> <h3>${elem.price}</h3> </div> <div id="btn"> <button id="${index}" class="twobtn">Cart</button> <h4>${elem.cartnumber}</h4> </div> </div>
})
document.querySelector("#main").innerHTML = temp;
}
功能购物车(){
main.addEventListener('click', function (dets) {
var id = dets.target.id;
// if (localStorage.getItem('msgs') === null) {
// updatelocalstorage(arr);
// 显示(arr);
// }
// 否则 {
// updatelocalstorage(id)
// show(id);
// }
arr[id].cartnumber++;
show();
})
}
//inserting data in table for bill//
function showbill() {
main.addEventListener('click', function (dets) {
var template = ``;
if ((dets.target.id) === arr[dets.target.id]) {
}
else {
arr.forEach(function (elem) {
template += ` <tr>
<td>1</td>
<td>${elem.name}</td>
<td>1</td>
<td>${elem.price}</td>
</tr>`
})
document.querySelector("table").innerHTML = template;
}
})
}
【问题讨论】:
-
请修复formatting并解释您的方法遇到的问题 -> How do I ask a good question?
-
请修剪您的代码,以便更容易找到您的问题。请按照以下指南创建minimal reproducible example。