簡體   English   中英

用javascript顯示html表格

[英]displaying html table with javascript

請是編程的新手,我真的需要你的幫助。 請問如何隱藏 HTML 表格,然后使用.JavaScript 使用按鈕顯示它?

使用 document.querySelector 獲取元素和hidden屬性以顯示和隱藏表格。 使用事件偵聽器並偵聽按鈕上的click事件:

 var table = document.querySelector("table"); table.hidden = true; document.querySelector("button").addEventListener("click", function(event) { table.hidden = false; });
 table { text-align: center; border-collapse: collapse; } td, th { padding: 0 5px; border: 1px solid black; }
 <table> <tr> <th>Day</th> <th>Rain</th> </tr> <tr> <td>1</td> <td>50 mm</td> </tr> <tr> <td>2</td> <td>21 mm</td> </tr> <tr> <td>3</td> <td>5 mm</td> </tr> </table> <button>Show</button>

您可以使用名為 jQuery 的庫非常輕松地完成此操作。

  • 在您的<head> ,放入<script src="code.jquery.com/jquery-latest.js></script>
  • 給你的<table>一個 id,像這樣: <table id="myTable">
  • 添加一個這樣的按鈕: <button onclick="$('#myTable').hide();">Hide</button>
  • 另一個像這樣: <button onclick="$('#myTable').show();">Show</button>

這將允許您切換表格的可見性。

我在@metarmask 發帖之前就開始寫這篇文章了——他/她的回答要好得多,所以你應該聽聽他/她的建議。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM