简体   繁体   English

谁能告诉我如何使用 javascript/jquery 将其放入三列表中? 产品、价格和折扣(计算 20%)

[英]Can anyone tell me how I could put this in three column table using javascript/jquery? Product, Price and Discount(calculating 20%)

Here is my code.这是我的代码。

 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>

Use a<table> element .使用<table>元素 <table> elements have a method called insertRow which adds a new row to the table, and the row elements <tr> have a method called insertCell which adds a new cell to the row element. <table>元素有一个名为insertRow的方法,它向表格添加一个新行,而行元素<tr>有一个名为insertCell的方法,它向行元素添加一个新单元格。 We'll use both of those to add the data to the table instead of accumulating an html string, like so:我们将使用这两者将数据添加到表中,而不是累积一个 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>

I used textContent to set the text of the newly added cell instead of innerHTML that can some problems.我使用textContent来设置新添加的单元格的文本,而不是可能出现问题的innerHTML

Try this code :试试这个代码:

 <body> <table id="prices"></table> <script> var title = ["Product", "Price", "Discount"]; 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 discount = prices.map(myFunction); function myFunction(value) { return ((value * 80) / 100).toFixed(2); } var toDisplay = ""; var t = ""; var i = 0; for (; i < products.length; i++) { if (i < title.length) { t += "<th>" + title[i] + "</th>"; } toDisplay += "<tr>" + "<td>" + products[i] + "</td>" + "<td>" + prices[i] + "</td>" + "<td>" + discount[i] + "</td>" + "</tr>"; } t = "<tr>" + t + "</tr>" + toDisplay; document.getElementById("prices").innerHTML = t; </script> </body>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试使用JQuery突出显示表行但不起作用,有人可以告诉我为什么以及如何解决它吗? - Trying to highlight table row using JQuery but not working, can anyone tell me why and how to fix it? 我在 Javascript 中使用本地存储。谁能告诉我如何在网页上显示本地存储的项目? - I am using Local Storage in Javascript.Can anyone tell me how to display the items of local Storage on the webpage? 任何人都可以告诉我,是否可以使用JavaScript生成PDF文件? - Can anyone tell me that is it possible to generate PDF files using javascript? 如何在 Shopify 上使用 JavaScript 或 jQuery 自动计算折扣金额 (X - Y) 并显示折扣百分比 [暂停] - How to automate calculating discount amount (X - Y) and show the percentage discount using JavaScript or jQuery on Shopify [on hold] 谁能告诉我这个 javaScript function 是如何工作的? - Can anyone tell me how this javaScript function works please? 有谁可以告诉我如何执行此自定义javascript函数? - Does anyone can tell me how this custom javascript function to execute? 谁能告诉我如何在HTML网页中包含价格行情记录器? - Can anyone tell me how can a price ticker be included in a html webpage? 谁能告诉我这是如何工作的? - Can anyone tell me how this works? 谁能告诉我,如何使用 Java 脚本代码将下面的 html 下拉值保存到表格中 - Can anyone please tell me, how to save below html drop down values into table by using Java script Code 通过使用JavaScript在表中进行迭代来计算总价 - Calculating total price by iterating in the table using JavaScript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM