开发者

In my case, How to highlight table row when mouseover?

In my index.html page, I have an empty table defined as following:

<body>
  ...
  <table width="500" border="0" cellpadding="1" cellspacing="0" class="mytable">
      <tr></tr>
  </table>
  <script src="my.js">&l开发者_运维技巧t;/script>
</body>

As you saw above, there is an JavaScript file my.js is included.

my.js(which is used to update the table row):

var items = ARRAY_OF_OBJECTS_FROM_SERVER; //e.g. items=[{'John', '023567'},{'Bill', '055534'},...];

//Each object element in the "items" array contain "name" and "phone" attribute.

var mytable = $('.mytable tr:first');
for(var i=0; i<items.length; i++){
   var obj = items[i];
   mytable.after("<tr>");
   mytable.after("<td>&nbsp;</td>");
   mytable.after(" <td>"+obj.name+"</td>");         
   mytable.after("<td>"+obj.phone+"</td>");
   mytable.after("</tr>");
}

I successfully get the dynamical table working, but when I try to add mouse hover effect on each row, I just failed. What I tried is by using CSS:

.mytable tr:hover
{ 
  background-color: #632a2a;
  color: #fff;
} 
        
       

I would like the mouse hover with color highlight effect to be working on IE 7+, firefox and chrome, what is the correct way to implement the table row mouse hover effect in my case??

----EDIT----

Here is my index.html page:

index.html

<!DOCTYPE HTML>
<html>
<head>
<script type="text/javascript" src="https://getfirebug.com/firebug-lite.js"></script>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>my test</title>
<link href="mystyle.css" rel="stylesheet" type="text/css" media="screen" />
</head>
 <body>
      <table width="500" border="0" cellpadding="1" cellspacing="0" class="mytable">
          <tr>
          </tr>
      </table>
  <script src="my.js"></script>
  </body>
</html>

--SOLUTION----

@manji 's solution solved my problem. That's change in JavaScript to use append instead of after inside for loop. Then, the CSS way of highlighting row is working.


You are writing the <td> outside of <tr> with this:

mytable.after("<tr>");
mytable.after("<td>&nbsp;</td>");
mytable.after(" <td>"+obj.name+"</td>");         
mytable.after("<td>"+obj.phone+"</td>");
mytable.after("</tr>");

For example, first one will add a <tr> and close it, then 3 closed <td>s before the <tr> and the last one is incorrect and will have no effect.

Try it this way and it will work:

mytable.after("<tr>"
+"<td>&nbsp;</td>"
+"<td>"+obj.name+"</td>"
+"<td>"+obj.phone+"</td>"
+"</tr>");

and it's better to use .append() (it will add the objects in their list order):

var mytable = $('.mytable');  // mytable selector is changed to select the table
                              // you can remove the empty <tr>
for(var i=0; i<items.length; i++){
   var obj = items[i];
   mytable.append("<tr>"
   +"<td>&nbsp;</td>"
   +"<td>"+obj.name+"</td>"
   +"<td>"+obj.phone+"</td>"
   +"</tr>");


Try the following:

.mytable tr:hover td
{ 
  background-color: #632a2a;
  color: #fff;
}

Given your list of browser support, CSS is the proper solution. It's important to note that the cells (<td>) cover the row (<tr>). So it's their background that you want to modify.


You're best bet is to use jquery's hover: Click Here

IE 7 did not have hover support on elements other than anchor tags. (or maybe that was just 6) either way, since you are using jquery already you can get your hover effect done easily.

$("tr").hover(
     function () {
        $(this).addClass('hover_class');
     }, 
     function () {
        $(this).removeClass('hover_class');
     }
);

Note: IE 7 will only allow :hover if you are running in HTML 4.01 STRICT for your doctype. Otherwise you need to use javascript to accomplish what you are looking to do.


If you cannot get the css solution to work use a delegate function to handle the dynamic rows.

$("table.mytable").delegate("tr", "hover", function(){
    $(this).toggleClass("hover");
});


jQuery:

$('.mytable tr').hover(function() {
  $(this).addClass('active');
}, function() {
  $(this).removeClass('active');
});

CSS:

.mytable tr.active td
{ 
  background-color: #632a2a;
  color: #fff;
} 

Check out the working example: http://jsfiddle.net/JpJFC/


Use jquery delegate method which is the best way to do it from performance point of view.

$(".mytable").delegate("tr", "mouseover", function(e) {

                $(this).addClass('mouseoverClass');

            });

$(".mytable").delegate("tr", "mouseout", function(e) {

                $(this).removeClass('mouseoverClass');

            });
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜