innerHTML 不更新

innerHTML not updating

我正在尝试更新元素的 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>

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

这应该有效,为什么?

您需要设置元素的 innerHTML 才能更新该元素。

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

应该可以

您的错误是使用了 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>