谁能告诉我如何使用 javascript/jquery 将其放入三列表中?

这是我的代码。


var products = ["Echo Dot", "Echo Plus", "Echo Auto", "Echo Show", "Echo Link"];

var prices = [49.99, 69.99, 129.99, 229.99, 1099.99];

toDisplay = "";

total = 0;


for (var i = 0; i < products.length; i++) {

  toDisplay += "<li>" + products[i] + " - " + prices[i] + "</li>";

  total += prices[i];

}


document.getElementById('prices').innerHTML = toDisplay;

<ol id="prices">

</ol>


皈依舞
浏览 131回答 2
2回答

慕侠2389804

使用一个<table>元素。<table>元素有一个名为 的方法insertRow,该方法将新行添加到表中,并且行元素<tr>有一个名为 的方法insertCell,该方法将新单元格添加到行元素。我们将使用这两个方法将数据添加到表中,而不是累积 html 字符串,如下所示:var products = ["Echo Dot", "Echo Plus", "Echo Auto", "Echo Show", "Echo Link"];var prices = [49.99, 69.99, 129.99, 229.99, 1099.99];var discountPercent = 20;var table = document.getElementById("the-table");for (var i = 0; i < products.length; i++) {  var row = table.insertRow();    row.insertCell().textContent = products[i];  row.insertCell().textContent = prices[i];  row.insertCell().textContent = ((100 - discountPercent) * prices[i] / 100).toFixed(2);}table {  border-collapse: collapse;}table td, table th {  border: 1px solid black;  padding: 5px 10px;}<table id="the-table">  <thead>    <tr>      <th>Product</th>      <th>Price</th>      <th>Price After 20% Discount</th>    </tr>  </thead></table>我曾经textContent设置新添加的单元格的文本,而不是设置innerHTML可能会出现一些问题。

手掌心

试试这个代码:<body>&nbsp; &nbsp; <table id="prices"></table>&nbsp; &nbsp; <script>&nbsp; &nbsp; &nbsp; var title = ["Product", "Price", "Discount"];&nbsp; &nbsp; &nbsp; var products = [&nbsp; &nbsp; &nbsp; &nbsp; "Echo Dot",&nbsp; &nbsp; &nbsp; &nbsp; "Echo Plus",&nbsp; &nbsp; &nbsp; &nbsp; "Echo Auto",&nbsp; &nbsp; &nbsp; &nbsp; "Echo Show",&nbsp; &nbsp; &nbsp; &nbsp; "Echo Link",&nbsp; &nbsp; &nbsp; ];&nbsp; &nbsp; &nbsp; var prices = [49.99, 69.99, 129.99, 229.99, 1099.99];&nbsp; &nbsp; &nbsp; var discount = prices.map(myFunction);&nbsp; &nbsp; &nbsp; function myFunction(value) {&nbsp; &nbsp; &nbsp; &nbsp; return ((value * 80) / 100).toFixed(2);&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; var toDisplay = "";&nbsp; &nbsp; &nbsp; var t = "";&nbsp; &nbsp; &nbsp; var i = 0;&nbsp; &nbsp; &nbsp; for (; i < products.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; if (i < title.length) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; t += "<th>" + title[i] + "</th>";&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; toDisplay +=&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "<tr>" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "<td>" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; products[i] +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "</td>" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "<td>" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; prices[i] +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "</td>" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "<td>" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; discount[i] +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "</td>" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "</tr>";&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; t = "<tr>" + t + "</tr>" + toDisplay;&nbsp; &nbsp; &nbsp; document.getElementById("prices").innerHTML = t;&nbsp; &nbsp; </script>&nbsp; </body>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript