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 spanにはどうすればよいですか?linkType1

現在私はやっています:

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()は (スパンである最初の次の兄弟を見つけます。aと ) の間に何かを挿入すると、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()代わりに使用しないのはなぜですか?DOMclosest() がツリーを横切るように

于 2012-04-02T08:06:41.663 に答える