0

我正在使用这个插件

<table>
<tr>
     <td>
     <a class="linkType1" href="google.com">
         Google
     </a>
     <span style="display: none;">Goooooooooooogle</span>
    </td>
</tr>

<tr>
    <td>
    <a class="linkType1" href="yahoo.com">
        Yahoo
    </a>
    <span style="display: none;">Yaaaaaaaaaaaaaaho</span>
    </td>
</tr>
</table>

如何选择closest spanof linkType1-anchors 显示为工具提示?

目前我正在做:

jQuery(document).ready( function() {
    jQuery("a.linkType1").tooltip({ 
        bodyHandler: function() { 
            alert(jQuery(this).closest("span").html()); // this alert is showing `null`
            return "hi"; // i need to setup the alerted content here
        }, 
        showURL: false 
    });
});
4

3 回答 3

4

您的span元素不是链接的祖先,因此closest(查找当前元素或其祖先上的选择器的匹配项)不是您要查找的内容。

根据您的标记,您可能想要next("span")(它会找到下一个兄弟,但前提是它是一个跨度;它不会与后续兄弟继续)或者可能nextAll("span").first()(它会找到第一个下一个兄弟,它是一个跨度,以防万一在 thea和 the之间放置一些东西span,它会找到所有后续的兄弟姐妹,nextAll然后通过first. 所以

// If the `span` will always be the very next sibling
alert(jQuery(this).next("span").html());

或者

// If you may put something between the `a` and the `span` at some point
alert(jQuery(this).nextAll("span").first().html());
于 2012-04-02T08:05:16.947 回答
2

closest()找到最近的元素。您正在寻找兄弟姐妹:

jQuery(this).next("span").html()
于 2012-04-02T08:03:46.557 回答
0

$(this)指的是bodyHandler函数。试试这个

alert(jQuery(linkType1).closest("span").html());

为什么不find()改用?当最接近()横穿DOM

于 2012-04-02T08:06:41.663 回答