我正在使用以下JS代码。
当我运行页面时,js代码工作正常,我希望发生的所有事件都发生了,但是在我试图设置内部htmls并使用£符号时,它出现在它之前?谁能明白为什么?
function displayBasket(){
basket = document.getElementById("basket");
string = "";
var basketStorage = localStorage.getItem("basket");
jsonBasket = JSON.parse(basketStorage);
var totalPrice = 0;
itemTotal = 0;
for (var property in jsonBasket){
var qPrice = jsonBasket[property ].quantity * jsonBasket[property ].cost;
var total = jsonBasket[property ].quantity;
string += "<section id='basketPageSection'>";
if(jsonBasket.hasOwnProperty(property )){
string += "<p>Item: " + jsonBasket[property ].name + "</p>";
string += "<p>Price:'£'" + jsonBasket[property ].cost + "</p>";
string += "<p>Quantity: " + jsonBasket[property ].quantity + "</p>";
}
totalPrice += qPrice;
itemTotal += total;
string += "</section>";
}
string += "<section class='basketSection'> <h3> Total Cost:'£'" + parseFloat(totalPrice).toFixed(2) + "</h3></section>"
basket.innerHTML = string;
displayQuant();
document.getElementById("calc").value=totalPrice;
}
function displayQuant(){
var displayQuant = document.getElementById("basketQuantity")
displayQuant.innerHTML = itemTotal;
}
window.addEventListener("load", displayBasket);