我正在尝试更新元素的innerHTML但没有获得任何输出。
每次点击炸玉米饼的图像时,应将炸玉米饼的价格添加到总数中。值是数字,而不是字符串。
感谢任何帮助,我很难过。
(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>
&#13;
答案 0 :(得分:1)
json.players.now
这预计会起作用吗?
您需要设置元素的json.players.online
才能更新该元素。
答案 1 :(得分:1)
var Total = document.getElementById("Total");
//...
var nbrTotal = parseInt(Total.textContent);
//...
Total.innerHTML=sumtxt.toString();
应该工作
答案 2 :(得分:0)
您的错误正在使用,TOTAL上的InnerHTML设置如此
var Total = document.getElementById("Total").textContent;
而不是仅仅使用innerHTML
var Total = document.getElementById("Total");
请参阅此处:点击#taco1
后,您可以将值添加到总计中。
试试吧:
(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);
console.log("sumtxt is a " + typeof(sumtxt));
console.log("value of sumtxt is " + sumtxt);;
console.log("sumtxt is a " + typeof((sumtxt).toString()));
console.log("value of sumtxt is " + (sumtxt).toString());;
document.getElementById("Total").innerHTML = sumtxt;
console.log(sumtxt);
}
}, 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>