我有这个页面.当我更改条目数时,新显示的条目的鼠标悬停失败.
<!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;"> </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>
解决方法
你需要事件授权.您不必将单独的侦听器附加到当前表中的每个行,而只将一个附加到容器(#table)并传入选择器(tr)以匹配事件目标(因为事件从表内部冒泡,除非停止使用stopPropagation()).
使用jQuery 1.7,你可以在jQuery< 1.7中使用$(容器).on(event,selectorString,func)函数,该函数等于$(container).delegate(event,func). 没有中间参数的调用on(如$(my_rows).on(event,func),将是$(my_rows).bind(‘mouSEOver’,func)的1.7当量,它只适用于当前在DOM中的元素.
jQuery(document).ready(function() { jQuery('#example'). on('mouSEOver','tr',function() { jQuery(this).find('span:first').show(); }). on('mouSEOut',function() { jQuery(this).find('span:first').hide(); }); });