jQuery DataTables mouseover issue

2019-01-25 23:34发布

问题:

I have this page. When I change the number of entries, newly visible entries' mouseover fails.

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script type="text/javascript" src="http://www.datatables.net/release-datatables/media/js/jquery.js"></script>
<script type="text/javascript" src="http://www.datatables.net/release-datatables/media/js/jquery.dataTables.js"></script>

<link href="http://www.datatables.net/release-datatables/media/css/demo_table.css" rel="stylesheet" type="text/css"/>

<style type="text/css">
    .edit {display: none}
</style>

<script type="text/javascript" charset="utf-8">
    $(document).ready(function() {
        $('#example').dataTable();
    } );
</script>

<script>
    jQuery(document).ready(function() {
        jQuery('#example tr').mouseover(function() {
            jQuery(this).find('span:first').show();
        }).mouseout(function() {
            jQuery(this).find('span:first').hide();
        });
    }); 
</script>
</head>

<body>
<table id="example" class="display">
    <thead>
        <tr>
            <th>A</th>
            <th>B</th>
            <th>C</th>
            <th width="24em;">&nbsp;</th>
        </tr>
    </thead>
    <tbody>
        <tr class="gradeA" >
            <td>a1</td>
            <td>b1</td>
            <td>c1</td>
            <td> <span class="edit"> <a href="#">EDIT</a> </span> </td>
        </tr>
        <tr class="gradeA" >
            <td>a2</td>
            <td>b2</td>
            <td>c2</td>
            <td> <span class="edit"> <a href="#">EDIT</a> </span> </td>
        </tr>
        <tr class="gradeA" >
            <td>a3</td>
            <td>b3</td>
            <td>c3</td>
            <td> <span class="edit"> <a href="#">EDIT</a> </span> </td>
        </tr>
        <tr class="gradeA" >
            <td>a4</td>
            <td>b4</td>
            <td>c4</td>
            <td> <span class="edit"> <a href="#">EDIT</a> </span> </td>
        </tr>
        <tr class="gradeA" >
            <td>a5</td>
            <td>b5</td>
            <td>c5</td>
            <td> <span class="edit"> <a href="#">EDIT</a> </span> </td>
        </tr>
        <tr class="gradeA" >
            <td>a6</td>
            <td>b6</td>
            <td>c6</td>
            <td> <span class="edit"> <a href="#">EDIT</a> </span> </td>
        </tr>
        <tr class="gradeA" >
            <td>a7</td>
            <td>b7</td>
            <td>c7</td>
            <td> <span class="edit"> <a href="#">EDIT</a> </span> </td>
        </tr>
        <tr class="gradeA" >
            <td>a8</td>
            <td>b8</td>
            <td>c8</td>
            <td> <span class="edit"> <a href="#">EDIT</a> </span> </td>
        </tr>
        <tr class="gradeA" >
            <td>a9</td>
            <td>b9</td>
            <td>c9</td>
            <td> <span class="edit"> <a href="#">EDIT</a> </span> </td>
        </tr>
        <tr class="gradeA" >
            <td>a10</td>
            <td>b10</td>
            <td>c10</td>
            <td> <span class="edit"> <a href="#">EDIT</a> </span> </td>
        </tr>
        <tr class="gradeA" >
            <td>a11</td>
            <td>b11</td>
            <td>c11</td>
            <td> <span class="edit"> <a href="#">EDIT</a> </span> </td>
        </tr>
        <tr class="gradeA" >
            <td>a12</td>
            <td>b12</td>
            <td>c12</td>
            <td> <span class="edit"> <a href="#">EDIT</a> </span> </td>
        </tr>
        <tr class="gradeA" >
            <td>a13</td>
            <td>b13</td>
            <td>c13</td>
            <td> <span class="edit"> <a href="#">EDIT</a> </span> </td>
        </tr>
        <tr class="gradeA" >
            <td>a14</td>
            <td>b14</td>
            <td>c14</td>
            <td> <span class="edit"> <a href="#">EDIT</a> </span> </td>
        </tr>
    </tbody>
</table>
</body>

回答1:

You need event delegation. Instead of attaching separate listeners to each of the rows currently in the table, you attach just one to the container (#table) and pass in a selector (tr) to match event targets (since events bubble up from inside the table unless stopped using stopPropagation()).

Using jQuery 1.7+ you can use the $(container).on(event, selectorString, func) function which is equal to $(container).delegate(event, selectorString, func) in jQuery <1.7.

Calling on() without the middle argument, like $(my_rows).on(event, func), would be the 1.7 equivalent of $(my_rows).bind('mouseover', func) which only applies to elements currently in the DOM.

jQuery(document).ready(function() {
    jQuery('#example').
      on('mouseover', 'tr', function() {
          jQuery(this).find('span:first').show();
      }).
      on('mouseout', 'tr', function() {
          jQuery(this).find('span:first').hide();
      });
}); 


回答2:

Try moving your function for the mouse over into the parameters of the .dataTable()

<script type="text/javascript" charset="utf-8">
    $(document).ready(function() {
        $('#example').dataTable(           
            jQuery('#example tr').mouseover(function() {
                jQuery(this).find('span:first').show();
            }).mouseout(function() {
                jQuery(this).find('span:first').hide();
            })  
        );  
    } );
</script>

I just tested it and it worked for me.



回答3:

When adding new elements dynamically that have pre-existing functions/events/actions that are already bound, the new elements will not automatically inherent the events/actions of their siblings. I recommend using jQuery for something like this.

For jQuery versions greater than 1.3 - use jQuery BIND() function: http://api.jquery.com/bind/

Description: This will map the data passed to your new event handlers needed

For jQuery versions 1.7 or higher - use jquery ON() function:

http://api.jquery.com/on/

For your code, if Jquery 1.7 try:

jQuery("#example tr").on({

  mouseenter: function(){
    jQuery(this).find('span:first').show();
  },
  mouseleave: function(){
    jQuery(this).find('span:first').hide();
  }
});