简体   繁体   English

如何在 html 中突出显示单击表格的行

[英]How to highlight row on click on table in html

I have a problem with table on HTML using django,我在 HTML 上使用 django 时遇到问题,

i want to Hightlight the row where clicked on and get it's index我想Hightlight显示单击的行并获取它的索引

HTML CODE: HTML 代码:

        <table class="table table-striped table-sm" id="tab1" >
          <thead>
            <tr>
              <th scope="col"></th>
              <th scope="col">Nº</th>
              <th scope="col">Header</th>
              <th scope="col">Header</th>
              <th scope="col">Header</th>
              <th scope="col">Header</th>
            </tr>
          </thead>
          <tbody class="post">
            {% for post in posts %}

            <tr>
              <td><input type="checkbox"></td>
              <td><b><count> </count></b></td>
              <td>{{ post.head1 }}</td>
              <td>{{ post.head2 }}</td>
              <td>{{ post.head3 }}</td>
              <td>{{ post.head4 }}</td>
            </tr>

            {% endfor %}
            
          </tbody>
        </table>

Part of style sheet using counter:使用计数器的样式表的一部分:

      body {
          counter-reset: section;
        }

        count::before {
          counter-increment: section;
          content: "  " counter(section) " ";
        }

I try to use javascript code from many blog but not work.我尝试使用许多博客中的 javascript 代码,但没有用。 I can't use getelementbyid because i can't create dynamic IDs for all rows我不能使用getelementbyid因为我不能为所有行创建动态IDs

You can use JavaScript to add an event listener to the table rows, and then use the this keyword to reference the row that was clicked on.您可以使用 JavaScript 为表格行添加事件监听器,然后使用this关键字来引用被点击的行。 You can then add a class to that row to highlight it and get its index.然后,您可以向该行添加 class 以突出显示它并获取其索引。 Here is an example of how you could do this:这是您如何执行此操作的示例:

var tableRows = document.querySelectorAll("#tab1 tbody tr");
for (var i = 0; i < tableRows.length; i++) {
    tableRows[i].addEventListener("click", function() {
        var current = document.getElementsByClassName("highlight");
        if (current.length > 0) {
            current[0].className = current[0].className.replace(" highlight", "");
        }
        this.className += " highlight";
        console.log(this.rowIndex);
    });
}

This will add an event listener to each row in the table, and when a row is clicked, it will add a class called "highlight" to the row, and log the index of the row to the console.这将为表中的每一行添加一个事件侦听器,当单击某行时,它会向该行添加一个名为“highlight”的 class,并将该行的索引记录到控制台。

You can also use the querySelectorAll method to select all rows and add a click event listener to them.您还可以使用 querySelectorAll 方法对 select 所有行添加点击事件监听器。

const rows = document.querySelectorAll("#tab1 tbody tr")
rows.forEach(row => {
    row.addEventListener("click", e => {
        rows.forEach(row => row.classList.remove("highlight"))
        e.currentTarget.classList.add("highlight")
        console.log(row.rowIndex + 1)
    })
});

This will add a click event listener to each row and remove the highlight class from all the rows and then add it to the one that was clicked on and then log the row index to the console.这将为每一行添加一个点击事件侦听器,并从所有行中删除突出显示 class,然后将其添加到被单击的行,然后将行索引记录到控制台。

You can also use jQuery, it's simple and effective too.也可以拨打jQuery,简单有效。

$("#tab1 tbody tr").click(function() {
  $("#tab1 tbody tr").removeClass("highlight");
  $(this).addClass("highlight");
  console.log($(this).index() + 1);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM