第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

使表計(jì)數(shù)行

使表計(jì)數(shù)行

慕尼黑8549860 2022-07-08 15:47:24
我有一個(gè)自動(dòng)添加行或刪除它們的表。我想讓表格計(jì)算這些行并給它們編號(hào)。例如;| # | 姓名 | 姓氏|| 1 | 杰克 | 默里 || 2 | 瑪麗亞| 棕色 |這是我的代碼;不用擔(dān)心 php 代碼。我只需要修復(fù)javascript。它可能不起作用,因?yàn)槲覜](méi)有將 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.    ?>
查看完整描述

2 回答

?
波斯汪

TA貢獻(xiàn)1811條經(jīng)驗(yàn) 獲得超4個(gè)贊

有點(diǎn)不清楚實(shí)際上問(wèn)題出在哪里 - 如果只是為每一行添加一個(gè)可以在 PHP 循環(huán)中輕松完成的數(shù)字(設(shè)置一個(gè)變量并在每次循環(huán)迭代時(shí)遞增。)


進(jìn)一步的混亂,因?yàn)楦鶕?jù)問(wèn)題,可以自動(dòng)添加或刪除行,但尚不清楚這是如何發(fā)生的或?yàn)槭裁磿?huì)發(fā)生。javascript 不正確,HTML 也不正確,因?yàn)槊總€(gè)都設(shè)置了重復(fù)的 ID 屬性TD~ 如果確實(shí)需要,一個(gè)更好的選擇可能是使用dataset屬性,但是可以使用querySelector&/or來(lái)完成定位 DOM 中的特定元素querySelectorAll


下面的代碼使用一段簡(jiǎn)單的 Javascript ( & querySelectorAll ) 來(lái)查找表格中所有相關(guān)的表格單元格,并為每個(gè)單元格分配一個(gè)整數(shù)。由于"table which automatically adds rows or removes them"如果添加或刪除了一行,整數(shù)將不再準(zhǔn)確 - 因此使用MutationObserver監(jiān)視 DOM 以了解對(duì)表的更改。


我希望這能提供一些幫助。


document.addEventListener('DOMContentLoaded',(e)=>{


    // specifically target ALL first cells within the table and assign some content - an integer.

    const callback = function() {

        Array.from( document.querySelectorAll( 'tr > td:first-of-type' ) ).forEach( ( td, i )=>{

            td.textContent=i + 1;

        })      

    };

    

    // determine what the observer will react to

    const config = { 

        attributes:false,

        childList:true,

        characterData:false,

        subtree:true

    };

    

    // monitor the table for changes

    ( new MutationObserver( callback ) ).observe( document.querySelector('table'), config );

    

    

    // number the table rows at page load

    callback();


    // utility function to find table row

    const getrow=function(e){

        let n=e.target;

        while(n.tagName!='TR')n=n.parentNode;

        return n;

    };

    

    // a simple event listener bound to the `delete` links which will remove entire table row

    // the `MutationObserver` will be triggered by a row deletion and the rows will be re-indexed

    Array.from( document.querySelectorAll('td > a') ).forEach( a=>{

        a.onclick=function(e){

            e.preventDefault()

            let tr=getrow(e);

            tr.parentNode.removeChild( tr );

        }

    })

})

<table>

    <thead>

        <tr>

            <th>#</th>

            <th>Name</th>

            <th>Surname</th>

            <th>Delete</th>

        </tr>

    </thead>

    <tbody>

        <tr>

            <td></td>

            <td>fred</td>

            <td>bloggs</td>

            <td><a href="#">Delete</a></td>

        </tr>

        <tr>

            <td></td>

            <td>jim</td>

            <td>smith</td>

            <td><a href="#">Delete</a></td>

        </tr>

        <tr>

            <td></td>

            <td>john</td>

            <td>doe</td>

            <td><a href="#">Delete</a></td>

        </tr>

        <tr>

            <td></td>

            <td>mike</td>

            <td>hunt</td>

            <td><a href="#">Delete</a></td>

        </tr>

    </tbody>

</table>


如果這有過(guò)于復(fù)雜的事情,并且您只需要在每一行上顯示一個(gè)數(shù)字并且表格在頁(yè)面加載后不會(huì)改變,那么要么使用前面提到的 PHP 添加,或者您甚至可以只使用 CSS


查看完整回答
反對(duì) 回復(fù) 2022-07-08
?
桃花長(zhǎng)相依

TA貢獻(xiàn)1860條經(jīng)驗(yàn) 獲得超8個(gè)贊

const rows = document.querySelectorAll 在 TR 上的效果怎么樣,然后 .length 將是下一個(gè)數(shù)字(因?yàn)轭^部的 tr 基本上是你通常會(huì)做的 +1 來(lái)增加數(shù)字)



查看完整回答
反對(duì) 回復(fù) 2022-07-08
  • 2 回答
  • 0 關(guān)注
  • 116 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)