使表计数行

我有一个自动添加行或删除它们的表。我想让表格计算这些行并给它们编号。例如;


| # | 姓名 | 姓氏|


| 1 | 杰克 | 默里 |


| 2 | 玛丽亚| 棕色 |


这是我的代码;


不用担心 php 代码。我只需要修复javascript。它可能不起作用,因为我没有将 php 代码放在表中。


var number = document.getElementById ( "nmr" ).innerText;

if (number = 1){

  number = number + 1;

}

<table class="table">

  <tr>

    <th>#</th>

    <th><b>Name</b></th>

    <th><b>Email</b></th>

    <th><b>The Lowest Money</b></th>

    <th><b>The Most Money</b></th>

    <th><b>Currency</b></th>

    <th><b>Age</b></th>

    <th><b>Gender</b></th>

    <th><b>Hobby 1</b></th>

    <th><b>Hobby 2</b></th>

    <th><b>Reason</b></th>

    <th><b>Favorite Color</b></th>

    <th></th>

    <th></th>

  </tr>

  

  <?php

  require 'inc/session.ic.php';


  $sql = "SELECT * FROM people WHERE username='" . $_SESSION[ "uid" ] . "'";

  $res = mysqli_query( $conn, $sql );



  ?>

  <?php

  while ( $row = mysqli_fetch_assoc( $res ) ) {


    $sql = "SELECT * FROM supportus WHERE emailUsers=\"" . $row["emailPerson"] ."\"";

    $res2 =  mysqli_query($conn, $sql);

      $giftExists = mysqli_num_rows($res2) > 0;

    // eger varsa, asagidaki butonu degistir.

    ?>


慕尼黑8549860
浏览 94回答 2
2回答

波斯汪

有点不清楚实际上问题出在哪里 - 如果只是为每一行添加一个可以在 PHP 循环中轻松完成的数字(设置一个变量并在每次循环迭代时递增。)进一步的混乱,因为根据问题,可以自动添加或删除行,但尚不清楚这是如何发生的或为什么会发生。javascript 不正确,HTML 也不正确,因为每个都设置了重复的 ID 属性TD~ 如果确实需要,一个更好的选择可能是使用dataset属性,但是可以使用querySelector&/or来完成定位 DOM 中的特定元素querySelectorAll下面的代码使用一段简单的 Javascript ( & querySelectorAll ) 来查找表格中所有相关的表格单元格,并为每个单元格分配一个整数。由于"table which automatically adds rows or removes them"如果添加或删除了一行,整数将不再准确 - 因此使用MutationObserver监视 DOM 以了解对表的更改。我希望这能提供一些帮助。document.addEventListener('DOMContentLoaded',(e)=>{&nbsp; &nbsp; // specifically target ALL first cells within the table and assign some content - an integer.&nbsp; &nbsp; const callback = function() {&nbsp; &nbsp; &nbsp; &nbsp; Array.from( document.querySelectorAll( 'tr > td:first-of-type' ) ).forEach( ( td, i )=>{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; td.textContent=i + 1;&nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; };&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; // determine what the observer will react to&nbsp; &nbsp; const config = {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; attributes:false,&nbsp; &nbsp; &nbsp; &nbsp; childList:true,&nbsp; &nbsp; &nbsp; &nbsp; characterData:false,&nbsp; &nbsp; &nbsp; &nbsp; subtree:true&nbsp; &nbsp; };&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; // monitor the table for changes&nbsp; &nbsp; ( new MutationObserver( callback ) ).observe( document.querySelector('table'), config );&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; // number the table rows at page load&nbsp; &nbsp; callback();&nbsp; &nbsp; // utility function to find table row&nbsp; &nbsp; const getrow=function(e){&nbsp; &nbsp; &nbsp; &nbsp; let n=e.target;&nbsp; &nbsp; &nbsp; &nbsp; while(n.tagName!='TR')n=n.parentNode;&nbsp; &nbsp; &nbsp; &nbsp; return n;&nbsp; &nbsp; };&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; // a simple event listener bound to the `delete` links which will remove entire table row&nbsp; &nbsp; // the `MutationObserver` will be triggered by a row deletion and the rows will be re-indexed&nbsp; &nbsp; Array.from( document.querySelectorAll('td > a') ).forEach( a=>{&nbsp; &nbsp; &nbsp; &nbsp; a.onclick=function(e){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.preventDefault()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let tr=getrow(e);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tr.parentNode.removeChild( tr );&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; })})<table>&nbsp; &nbsp; <thead>&nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>#</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>Name</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>Surname</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>Delete</th>&nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; </thead>&nbsp; &nbsp; <tbody>&nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td></td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>fred</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>bloggs</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td><a href="#">Delete</a></td>&nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td></td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>jim</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>smith</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td><a href="#">Delete</a></td>&nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td></td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>john</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>doe</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td><a href="#">Delete</a></td>&nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td></td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>mike</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>hunt</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td><a href="#">Delete</a></td>&nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; </tbody></table>如果这有过于复杂的事情,并且您只需要在每一行上显示一个数字并且表格在页面加载后不会改变,那么要么使用前面提到的 PHP 添加,或者您甚至可以只使用 CSS

桃花长相依

const rows = document.querySelectorAll 在 TR 上的效果怎么样,然后 .length 将是下一个数字(因为头部的 tr 基本上是你通常会做的 +1 来增加数字)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript