gpt4 book ai didi

javascript - innerHTML 不更新

转载 作者:行者123 更新时间:2023-11-30 16:36:09 24 4
gpt4 key购买 nike

我正在尝试更新元素的 innerHTML 但没有得到任何输出。

每次点击炸 Jade 米饼的图像时,炸 Jade 米饼的价格都应添加到总价中。这些值是数字,而不是字符串。

感谢任何帮助,我很难过。

(function() {

var T1 = document.querySelector('#taco1');
T1.addEventListener("click", function(e) {
if (e.target.tagName === "IMG") {
var Total = document.getElementById("Total").textContent;
var price = document.getElementById("p1").textContent;
var nbrprice = parseInt(price);
var nbrTotal = parseInt(Total);
console.log("value of nbrprice is " + nbrprice)
console.log("nbrprice is a " + typeof(nbrprice));
console.log("nbrTotal is a " + typeof(nbrTotal));
var sum = Number(price) + Number(Total);
console.log("sum is a " + typeof(sum));
console.log("value of sum is " + sum);;
}
var sumtxt = (sum + nbrprice);
Total.innerHTML = sumtxt.toString();
console.log(Total);
}, false);

})();
<div id='taco1'>
<img src="https://placehold.it/300x100" width="300" />
<p>Taco One Price: <span id="p1">4</span>
</p>
</div>
<div id='taco2'>
<img src="https://placehold.it/300x100" width="300" />
<p>Taco Two Price: <span id="p2">5</span>
</p>
</div>

<div>Total: $<span id="Total">0</span>
</div>

最佳答案

var Total = document.getElementById("Total");
//...
var nbrTotal = parseInt(Total.textContent);
//...
Total.innerHTML=sumtxt.toString();

应该可以

关于javascript - innerHTML 不更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32672745/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com