我正在使用此plugin:
<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
});
});
答案 0 :(得分: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());
答案 1 :(得分:2)
closest()
找到最近的父元素。你正在寻找一个兄弟姐妹:
jQuery(this).next("span").html()
答案 2 :(得分:0)
$(this)
指的是bodyHandler函数。
试试这个
alert(jQuery(linkType1).closest("span").html());
为什么不使用find()
呢?最近()横穿DOM
树