我无法用一个变量失去它的价值,我无法找到出现这种情况的原因,也许我错过的东西在这里..的js变量失去它的价值没有道理
function addToCart(item){
if(cartItems === undefined){
alert("nulling");
var cartItems = 0;
}
var info = ["Nacho Sauchelli - Morgen 9", "Gaetano C - Airport", "Carmello Gargaglione Situation", "Irony (Paul DC Remix", "Proudly People - In the Factory", "Clap Contageon - Yellow Cup", "Fantini - Ricalibrae everything", "Dani rivas - Scroll", "Ben Grunnely - Bass Valve"];
if(localStorage.getItem("username") == ""){
alert("Cannot order, not logged in!");
}
else{
var items = 9;
var i = 0;
for(i=0;i<items;i++){
if(i == item){
var meme = document.getElementsByClassName("album");
if(meme[i].style.opacity == '0.75'){
meme[i].style.opacity = 1;
cartItems--;
}else{
meme[i].style.opacity = .75;
meme[i].style.color = 'dodgerblue';
cartItems++;
}
}
}
itemsInCart(cartItems);
}
}
function itemsInCart(cartItems){
var cth = document.querySelector("#cartItemsHolder");
cth.innerHTML = "Items In Cart:"+cartItems+"";
}
,每天进入未定义检查一次,我试着写了许多其他方面的发言,但我终于想通了,问题是其他地方
真的吗?看看你的代码的前两行。你使用它的第一个地方是在if语句中,所以它当然是未定义的。 –
@EdCottrell我怀疑OP的意图是变量是全局变量。 – Pointy
@Pointy很可能,但这个问题显示没有真正努力去理解问题或提供一个MCVE。 –