简体   繁体   中英

How do I highlight a row by javascript onClick Event method?

I've a list of data from db which I'm showing in each row on a Table. I want when I click a row it's should be remain highlighted with color. But my following code is highlighting all row. Can you help me about it.

It's should be highlight only one row which I clicked.

My code

<script type="text/javascript">
function visited(a) {
tr = a.parentNode.parentNode;
tr.style.backgroundColor = "red";
}
</script>

<?php
    echo "<table width='100%' cellpadding='0' cellspacing='0'>";
    echo "<thead>";
    echo "<tr>";                
    echo "<td class='' valign='top' width='200'></td>";
    echo "<td class='' valign='top' width='125'></td>";                             
    echo "<td class='' valign='top' width='125'></td>";                             
    echo "<td class='' valign='top' width='125'></td>";                             
    echo "<td class='' valign='top' width='125'></td>";                             
    echo "</tr>";   
    echo "</thead>";
    echo "<tbody>";             

while($res =  mysql_fetch_array($get)){
    $cdid = $res['cdid'];
    $family_name = $res['family_name'];
    $given_name = $res['given_name'];
    $work_phone = $res['work_phone'];
    $mobile_phone = $res['mobile_phone'];
    $email = $res['email'];
    $email_private = $res['email_private'];
    $cid = $res['cid'];
    $department = $res['department'];
    $title = $res['title'];

    $getComapnyName =  mysql_query("SELECT company_name FROM company WHERE cid = '$cid' ");
    $resCompany =  mysql_fetch_array($getComapnyName);
    $companyName =  $resCompany['company_name'];

    if (strlen($companyName) >= 20) {
        $companyName =  substr($companyName, 0, 10). " ... " . substr($companyName, -5);
    }else{
        $companyName = $companyName;
    }           
    echo "<tr onclick='getDetails($cdid),showNotexBox($cdid),showAllNotesBox($cdid), visited(this);'>";                             
    echo "<td class='' valign='top'>$companyName</td>";
    echo "<td class='' valign='top'>$family_name</td>";
    echo "<td class='' valign='top'>$given_name</td>";
    echo "<td class='' valign='top'>$department</td>";
    echo "<td class='' valign='top'>$title</td>";

    echo "</td>";
    echo "</tr>";
    }
    echo "</tbody>";
    echo "</table>";    
?>

If you want to go with jQuery you can do the following. Just copy paste the code and run it

  <html>
     <head>
      <meta charset="UTF-8">
      <title></title>
      <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">  </script>

      <script type="text/javascript">


    var Color = {
        initialize: function() {
                $(document).on("click", ".trow", Color.setColor)                                                
        },
        setColor: function() {

            $(".trow").css('background-color','#fff');
            this.style.backgroundColor = 'red';            
        }
    };          
    $(document).ready(function(){
        Color.initialize();
    });     
    </script>        
   </head>
   <body>
   <table width='100%' cellpadding='0' cellspacing='0'>
   <tbody>                
   <tr class='trow' id='r1'>                             
    <td class='' valign='top'>value1</td>
    <td class='' valign='top'>value2</td>
    <td class='' valign='top'>value3</td>
    <td class='' valign='top'>value4</td>
    <td class='' valign='top'>value5</td>
   </tr>
   <tr class='trow' id='r2'>                             
    <td class='' valign='top'>price1</td>
    <td class='' valign='top'>price2</td>
    <td class='' valign='top'>price3</td>
    <td class='' valign='top'>prive4</td>
    <td class='' valign='top'>price5</td>
   </tr>    
   </tbody>
   </table>
   </body>
   </html>

Anyway, you can add or remove classes with jQuery and use css for styling

Since your a argument is referencing the <tr> being clicked, by using a.parentNode.parentNode you're actually changing the backgroundColor of the table.

Try this instead:

function visited(tr) {
    tr.style.backgroundColor = "red";
}

As per your edit, try this:

function visited(tr) {
    var table = tr.parentNode.parentNode;
    var trs = table.getElementsByTagName('tr');

    for (var i = 0; i < trs.length; i++)
        {
        trs[i].style.backgroundColor = null;
        trs[i].style.color = null;
        }

    tr.style.backgroundColor = '#000';
    tr.style.color = '#fff';
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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