如何使用 jQuery 將表格用 div 元素包裹起來?


若要將表格用 div 元素包裹起來,請使用 wrap() 方法。可以嘗試執行以下程式碼,以使用 jQuery 將表格用 div 元素包裹起來 -

示例

線上演示

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
 
     $("#button1").click(function(){
        $('table').wrap('<div></div>');
    });
   
});
</script>
<style>
 div {
  background-color: gray;
 }
</style>
</head>
<body>

 <table style="width:100%">
  <tr>
    <th>Firstname</th>
    <th>Lastname</th>
    <th>Age</th>
  </tr>
  <tr>
    <td>Will</td>
    <td>Smith</td>
    <td>50</td>
  </tr>
  <tr>
    <td>Eve</td>
    <td>Jackson</td>
    <td>94</td>
  </tr>
</table>

<button id="button1">Wrap</button>

</body>
</html>

更新日期:2020 年 2 月 14 日

733 次瀏覽

開啟您的職業生涯

完成課程獲得認證

立即開始
廣告