简体   繁体   English

按下按钮时,如何将数组作为表格显示到 HTML 页面?

[英]How do I display an array to a HTML page as a table when a button is pressed?

I have a project due and specific things are needed.我有一个项目到期,需要具体的东西。 I am trying to display a table of an array to a HTML page from a js file when a button is pressed it should display the table.我试图在按下按钮时从 js 文件将数组表显示到 HTML 页面,它应该显示该表。 Here's what I got so far:这是我到目前为止得到的:

 function dundrumArray(){ var dunMenu={ OilleánTub:"¢3.50", DeluxeOilleánTub:"¢3.80", OilleánTubSmall:"¢2.50", }; //printout object key:value in a table document.write("<table id=myTable><tr><th>Name</th><th>Cost</th></tr>"); for(var key in dunMenu) { document.write("<tr><td>",key,"</td><td>",dunMenu[key],"</td></tr>"); }; document.write("</table>"); } function cityArray(){ var cityMenu={ CitrusCrepe:"¢3.50", NutellaCrepe:"¢4", StrawberryChoc:"¢2.50", }; //printout object key:value in a table document.write("<table><tr><th>Name</th><th>Cost</th></tr>"); for(var key in cityMenu) { document.write("<tr><td>",key,"</td><td>",cityMenu[key],"</td></tr>"); }; document.write("</table>"); } function orderOnline(){ var orderMenu={ BuenoWaffleCone:"¢4.50", OilleánMess:"¢5.50", OilleánNoffee:"¢5.50", SnickerBockerGlory:"¢5.50", TwixFix:"¢5.50", }; //printout object key:value in a table document.getElementById("myTable"); document.write("<table><tr><th>Name</th><th>Cost</th></tr>"); for(var key in orderMenu) { document.write("<tr><td>",key,"</td><td>",orderMenu[key],"</td></tr>"); }; document.write("</table>"); }
 </header> <div class="button-group"> <button class="button button-primary" onclick="dundrumArray()">Dun Drum</button> <button class="button button-primary" onclick="cityArray()">City Center</button> <button class="button button-primary" onclick="orderOnline()">Order Online</button> </div> <div id="myTable"></div> </main>

Currently, when I push one of the buttons it jumps to a new page.目前,当我按下其中一个按钮时,它会跳转到一个新页面。 I'm quite new to this sort of stuff.我对这种东西很陌生。 Any help would be appreciated任何帮助,将不胜感激

You need to replace the innerHTML of the target element with text, like this:您需要将目标元素的 innerHTML 替换为文本,如下所示:

 function cityArray(){ var cityMenu={ CitrusCrepe:"¢3.50", NutellaCrepe:"¢4", StrawberryChoc:"¢2.50", }; //printout object key:value in a table let txt = "<table><tr><th>Name</th><th>Cost</th></tr>"; for(var key in cityMenu) { txt += "<tr><td>" + key + "</td><td>" + cityMenu[key] + "</td></tr>"; }; document.getElementById('myTable').innerHTML = txt; }
 <div class="button-group"> <button type="button" class="button button-primary" onclick="cityArray()">City Center</button> </div> <div id="myTable"></div>

thereis plenty JS methods to deal withe tables:有很多 JS 方法来处理表格:

 const menu = { dunMenu: { OilleánTub: "¢3.50", DeluxeOilleánTub: "¢3.80", OilleánTubSmall: "¢2.50" }, cityMenu: { CitrusCrepe: "¢3.50", NutellaCrepe: "¢4", StrawberryChoc: "¢2.50" }, orderMenu: { BuenoWaffleCone: "¢4.50", OilleánMess: "¢5.50", OilleánNoffee: "¢5.50", SnickerBockerGlory: "¢5.50", TwixFix: "¢5.50" } } const div4Table = document.querySelector('div#myTable'), byMenu = document.querySelector('div.button-group'); function makeTable(tbName) { div4Table.innerHTML = "" // clear Anything let t = div4Table.appendChild( document.createElement('table')), tH = t.createTHead(), tB = t.createTBody(); nH = tH.insertRow() nH.insertCell().textContent = 'Name' nH.insertCell().textContent = 'Cost' Object.entries( menu[tbName]).forEach(([k,v])=> { let nRow = tB.insertRow() nRow.insertCell().textContent = k nRow.insertCell().textContent = v }) }
 table { border-collapse: collapse; margin: .5em; } thead { font-weight: bold; background-color: #978aec; text-align: center; } td { padding: .2em.7em; border: 1px solid black; }
 <div class="button-group"> <button class="button button-primary" onclick="makeTable('dunMenu')">Dun Drum</button> <button class="button button-primary" onclick="makeTable('cityMenu')">City Center</button> <button class="button button-primary" onclick="makeTable('orderMenu')">Order Online</button> </div> <div id="myTable"></div>

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

相关问题 在HTML表中按下按钮时,如何获取行和列的值? - How do I get the row and column values when a button is pressed in a HTML table? 当按下HTML按钮时,如何显示从PHP函数返回的数据 - How can I display data returned from a PHP function when a HTML button is pressed 按下后退按钮时如何使Firefox重新加载页面? - How do I make Firefox reload page when back button is pressed? 在 HTML 代码中按下按钮时,如何触发 JavaScript function - How do I trigger a JavaScript function when a button is pressed in HTML code 我想在按下按钮时显示元素。 默认情况下显示该元素,如何使其默认不显示? - I want to make an element display when a button is pressed. by default the element is shown, how do I make it display none by default? 在HTML上按下按钮后,如何重新创建重新加载问题 - How can I recreate the reload issue when a button is pressed on HTML 如何捕获何时按下了什么按钮的序列? - How do I capture a sequence of what button was pressed and when? 按下按钮时如何添加通知? - how do I add a notification when a button is pressed? 按下按钮时如何不刷新页面 - How to not refresh a page when a button is pressed 按下按钮时如何模拟按下的不同按钮? - How can I simulate a different button pressed when a button is pressed?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM