简体   繁体   English

jQuery向表行添加类仅适用于第一行

[英]Jquery to add class to table row only applies to first row

I am trying to use jquery to add a class to the table row element but it only works on the first row. 我正在尝试使用jquery将一个类添加到表行元素,但它仅适用于第一行。 I am populating the table using a mysql database and using a while loop to display the table. 我正在使用mysql数据库填充表,并使用while循环显示表。

After pressing the button I want each row to change color when hovered over. 按下按钮后,我希望每行悬停时都改变颜色。 The class I want applied to the table row element: 我要应用于表格行元素的类:

.hoverChange:hover
 {

     background-color: #66ccff

 }

The table and while loop which displays all the data: 表和while循环显示所有数据:

               <table border = 1> 
                <tr>
                <th>  ID  </th>
                <th>  Name  </th>
                <th>  Description  </th>
                <th>  Type  </th>
                <th>  Price  </th>
                <th>  Cost Price  </th>
                <th>  Stock Level </th>
                <th>  EAN  </th>
                </tr> 

                 <?php

                 while ($product_Info = mysqli_fetch_array($result,MYSQLI_ASSOC)) {


                         ?>

                        <tr id = tableRows>

                         <?php
                         echo "<td>" . $product_Info["product_ID"] . "</td>";
                         echo "<td>" . $product_Info["product_Name"] . "</td>";
                         echo "<td>" . $product_Info["product_Desc"] . "</td>";
                         echo "<td>" . $product_Info["product_Type"] . "</td>";
                         echo "<td>" . $product_Info["product_Price"] . "</td>";
                         echo "<td>" . $product_Info["product_Cost"] . "</td>";
                         echo "<td>" . $product_Info["product_Stock"] . "</td>";
                         echo "<td>" . $product_Info["product_ean"] . "</td>">                          


                         echo "<tr>";

                 }

                echo "</table>";

The button: 按钮:

<button type="button" id = "updateBtn" class="btn btn-info">Update</button>                 

And finally the jquery I am using: 最后是我正在使用的jQuery:

$(document).ready(function(){
$("#updateBtn").click(function(){
    $('#tableRows').addClass('hoverChange');
});
 });

You need to use a class instead of an ID for your table rows. 您需要为表行使用类而不是ID。 ID's Must Be Unique , specifically because it will cause problems in JavaScript and CSS when you try to interact with those elements. ID的“必须唯一” ,特别是因为当您尝试与这些元素进行交互时,它将在JavaScript和CSS中引起问题。

You have some stray spaces and you're not quoting attributes. 您有一些杂散的空格,并且没有引用属性。 This could also become problematic in the future, ie 将来这也可能成为问题,即

<tr class="myClass">

You should use class instead of id attribute for repeating objects. 您应该使用class而不是id属性来重复对象。 ID should be unique for the whole page. ID在整个页面上应该是唯一的。

And be careful with spaces in your code ;) 并注意代码中的空格;)

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

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