更改 JS 中 IF 语句的 Html

所以我的代码是关于当我选择货币时价格和符号必须改变的;如果我选择 USD $ 并且它已经是 $ 那么什么都不会改变,而如果我选择 LBP 并且货币是 $ 价格必须乘以 2000 并且符号将从 $ 更改为 LBP,但问题是当我在 JS 中引入此语句后,脚本停止工作(将其乘以 2000):


function report(currencyy) {

  var price = document.getElementById("price");

  var symbol = document.getElementById("symbol");

  symbol = String(symbol);


  if (currencyy == "lbp") {

    if (symbol =="$") {

      symbol.innerHTML = "lbp";

      var text = price.textContent;

      var number = parseInt(text) * 2000;

      price.innerHTML = number;

    }

  }

}

<pre> <div class="card">

  <img class="imgcar" src="cars/402088-2020-land-rover-range-rover-velar.jpg" alt="Avatar" style="width:16em">

  <div class="container">

    <h4><b>Range Rover Velar</b><br><b>4 Doors</b><br><b> 5 Passengers</b> </h4>

    <p id="price">100</p><p id="symbol">$</p><p>/24hrs</p>

  </div>

</div>

</pre>


噜噜哒
浏览 96回答 2
2回答

RISEBY

您应该比较内容而不是元素。另外,不确定您要做什么:symbol = String(symbol);尝试var price = document.getElementById("price");var symbol = document.getElementById("symbol");if (currencyy == "lbp") {&nbsp; if (symbol.textContent == "$") { //compare the content&nbsp; &nbsp; symbol.textContent = "lbp";&nbsp; &nbsp; var text = price.textContent;&nbsp; &nbsp; var number = parseInt(text) * 2000;&nbsp; &nbsp; price.textContent = number;&nbsp; }&nbsp;}}

繁星点点滴滴

<ng-template&nbsp; [ngIf]="isStatusCode[j] === false">&nbsp; &nbsp;<img class="list-image"&nbsp; (error)="changeImgSource($event)" [src]="getConfigProductImage(element['shrt-campaign-product'][0]['products']['product_images'])"&nbsp; &nbsp;height="40"&nbsp; &nbsp;alt="productImage"></ng-template><ng-template&nbsp; [ngIf]="isStatusCode[j] === true">&nbsp; &nbsp;<img class="list-image"&nbsp; (error)="changeImgSource($event)" src="/assets/images/loader.gif"&nbsp; height="60" alt="noImage"></ng-template>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript