admin管理员组文章数量:1422317
I am doing with this plugin :
<table>
<tr>
<td>
<a class="linkType1" href="google">
Google
</a>
<span style="display: none;">Goooooooooooogle</span>
</td>
</tr>
<tr>
<td>
<a class="linkType1" href="yahoo">
Yahoo
</a>
<span style="display: none;">Yaaaaaaaaaaaaaaho</span>
</td>
</tr>
</table>
How can I select the closest
span
of linkType1
-anchors for displaying as tooltip ?
Currently I am doing :
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
});
});
I am doing with this plugin :
<table>
<tr>
<td>
<a class="linkType1" href="google.">
Google
</a>
<span style="display: none;">Goooooooooooogle</span>
</td>
</tr>
<tr>
<td>
<a class="linkType1" href="yahoo.">
Yahoo
</a>
<span style="display: none;">Yaaaaaaaaaaaaaaho</span>
</td>
</tr>
</table>
How can I select the closest
span
of linkType1
-anchors for displaying as tooltip ?
Currently I am doing :
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
});
});
Share
Improve this question
asked Apr 2, 2012 at 8:01
tusartusar
3,4346 gold badges39 silver badges61 bronze badges
3 Answers
Reset to default 4Your span
elements aren't ancestors of the links, so closest
(which looks for a match for the selector on the current element or its ancestors) isn't what you're looking for.
Based on your markup, you might want next("span")
(which will find the next sibling, but only if it's a span; it doesn't continue with subsequent siblings) or possibly nextAll("span").first()
(which will find the first next sibling that's a span, in case you ever put something between the a
and the span
), which finds all subsequent siblings with nextAll
and then grabs the first of them (the one nearest the link) via first
. So
// If the `span` will always be the very next sibling
alert(jQuery(this).next("span").html());
or
// If you may put something between the `a` and the `span` at some point
alert(jQuery(this).nextAll("span").first().html());
closest()
finds the closest parent element. You are looking for a sibling:
jQuery(this).next("span").html()
$(this)
is referring to bodyHandler function.
Try this
alert(jQuery(linkType1).closest("span").html());
Why not use find()
instead? As closest() transverses up the DOM
tree
本文标签: javascriptjQueryclosest() gives quotnullquot valueStack Overflow
版权声明:本文标题:javascript - jQuery.closest() gives "null" value - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744698327a2620415.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论