1

table row我想在鼠标悬停时显示位于每个 div 上的 div 。这样div.tools悬停的table row将在悬停时显示和隐藏。

这是我的桌子。

<table width="99%" cellspacing="0" cellpadding="2" class="main-box">
 <tbody><tr class="dataTableHeadingRow">
 <td class="dataTableHeadingContent">Categories / Products</td>
 <td class="dataTableHeadingContent" align="right">Action&nbsp;</td>
 </tr>

 <tr id="product_37">
   <td class="dataTableContent">
    <a href="#">Cotton Piqué Tennis Shirt</a>
    <div class="tools" style="display:none;">Tools Here</div>
   </td>
   <td class="dataTableContent" align="right">
    <img src="#/images/icon_arrow_right.gif" border="0" alt="">&nbsp;
   </td>
 </tr>

 <tr id="product_39">
   <td class="dataTableContent">
    <a href="#">Leather Plus Cotton Cut-Out Sling Back Sandal</a>
    <div class="tools" style="display:none;">Tools Here</div>
   </td>
   <td class="dataTableContent" align="right">
    <a href="#"><img src="#/images/icon_info.gif" border="0" alt="Info" title=" Info "></a>&nbsp;
   </td>
  </tr>

  <tr id="product_38">
    <td class="dataTableContent">
     <a href="#">Poly-Cotton 3/4 Sleeve Raglan Shirt</a>
    <div class="tools" style="display:none;">Tools Here</div>
    </td>
    <td class="dataTableContent" align="right">
     <a href="#">
     <img src="#/images/icon_info.gif" border="0" alt="Info" title=" Info "></a>&nbsp;
    </td>
  </tr>

  </tbody>
</table>

我尝试了这个 jQuery 函数,但它不起作用。

jQuery(function() {
 jQuery('*[id^=product_]').hover(function(){
   jQuery(this).children("div.tools").show();
    },function(){
   jQuery(this).children("div.tools").hide();
    });
 });
});
4

1 回答 1

3

使用.find()而不是.children().

您使用的.children()方法仅找到直接子级,在您的 tr 元素的情况下将是 tds。

.find()方法在所有后代中查找,因此它将在 tds 内部查找您的 div。

您还遇到了一个语法错误,导致您的代码完全停止工作:最后有一个额外的关闭});应该被删除。

此外,与其使用 选择所有元素,不如仅选择*该表中的 tr 元素可能更有效:

jQuery(function() {
    jQuery('table.main-box tr[id^=product_]').hover(function() {
        jQuery(this).find("div.tools").show();
    }, function() {
        jQuery(this).find("div.tools").hide();
    });
});​
于 2012-08-05T02:13:23.943 回答