0

我正在构建一个从数据库中获取和显示数据的动态表,表的结构如下所示

<table class="table" id="report">
  <thead>
    <tr>
      <th>Title</th>
      <th>Skill</th>
      <th>Area</th>
    </tr>
  </thead>

  <tbody>
    <?
      $sql="SELECT * from tablename ";
      $result= mysqli_query($con, $sql);
      if(mysqli_num_rows($result)>0) {
        while($row = mysqli_fetch_assoc($result)) {
          <tr>
            <td><? echo $title; ?></td>
            <td><? echo $skill; ?></td>
            <td><? echo $area; ?></td>
          </tr>
        }
      }
  </tbody>
</table>

我希望当用户单击标题时,视图应该展开,然后在另一次单击时它应该折叠。

我在jsfiddle中构建了一个静态表,它工作正常,但是当我尝试将代码与上表合并时,它不起作用。谁能告诉我哪里出错了?

4

1 回答 1

0

试试这个JS Fiddle,希望它能起作用,我在里面添加了虚拟文本,td然后点击td切换子 div 的可见性.hidden

$(document).ready(function () {
    $('.expandable').on('click', function(){
    	$(this).children('.hidden').toggle();
    });
});
.hidden {
    display:none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<table id="report" border="1" style="width:100%" >
    <tr>
        <th> First </th>
        <th> Second </th>
        <th> Third </th>
    </tr>

    <tr>
         <td class="expandable">1st title
            <div class="hidden">dummy text 1
                <br>dummy text 1
                <br>dummy text 1
                <br>
            </div>
        </td>
         <td>1</td>
         <td>1</td>
    </tr> 
    <tr>
         <td class="expandable">2st title
            <div class="hidden">dummy text 2
                <br>dummy text 2
                <br>dummy text 2
                <br>
            </div>
        </td>
         <td>2</td>
         <td>2</td>
    </tr>
</table>

于 2015-11-21T06:11:29.043 回答