jquery 最接近的不想工作
我在页面中重复了一个图像,旁边有一个 div,如下所示:
<img src="/img/productspage/questionMark.jpg" class="prodQuestionMark" />
<div class="vuQuestionBubble">
<p>this is where text for the text will go</p>
</div>
vuQuestionBubble
的样式为 display:none 默认情况下。单击“prodQuestionMark”时,我希望显示旁边的 vuQuestionBubble。我把这段代码放在底部。
$(document).ready(function () {
$('.prodQuestionMark').click(function () {
$(this).closest('vuQuestionBubble').show();
});
});
它似乎不起作用...单击事件正在工作,我可以使用 .parent 选择父 div,但似乎无法与最近的 div 交互...有什么想法吗?
I have repeated down the page an image with a div next to it like this:
<img src="/img/productspage/questionMark.jpg" class="prodQuestionMark" />
<div class="vuQuestionBubble">
<p>this is where text for the text will go</p>
</div>
vuQuestionBubble
has a style display:none by default. when 'prodQuestionMark' is clicked i want the vuQuestionBubble next to it to show. ive put this code at the bottom.
$(document).ready(function () {
$('.prodQuestionMark').click(function () {
$(this).closest('vuQuestionBubble').show();
});
});
it doesn't seem to work... the click event is working and i can select the parent div with .parent but cant seem to interact with the closest div... any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
closest
寻找祖先,而不是兄弟姐妹;另外,您的选择器在开头缺少.
(您告诉它寻找vuQuestionBubble
元素,其中您真正的意思是div
类“vuQuestionBubble”)。根据您当前的结构,您可以使用
next
因为带有“vuQuestionBubble”的 div 是最重要的下一个元素。但是,如果您更改了结构并在它们之间放置了一些东西,next
将不再适合您。我可能会使用
nextAll("div.vuQuestionBubble:first")
或nextAll(".vuQuestionBubble:first")
(链接:nextAll
,:first
):这将找到第一个类为“vuQuestionBubble”的 div,它作为同级跟随在
img
后面,即使它不是那个 正确的 位于img
旁边,因此如果标记稍有更改,您的代码就不易受到维护问题的影响。closest
looks for ancestors, not siblings; also, your selector is missing a.
at the beginning (you're telling it to look for avuQuestionBubble
element, where you really mean adiv
with the class "vuQuestionBubble").With your current structure, you can use
next
because the div with the "vuQuestionBubble" is the very next element. However, if you ever change your structure and put something between them,next
won't work for you.I'd probably use
nextAll("div.vuQuestionBubble:first")
ornextAll(".vuQuestionBubble:first")
(links:nextAll
,:first
):That will find the first div with the class "vuQuestionBubble" that follows the
img
as a sibling, even if it's not the one right next to theimg
, and so makes your code less susceptible to maintenance issues if the markup changes slightly.closest
函数查找距离该元素最近的祖先。您实际上需要使用.next('.vuQuestionBubble')
。The
closest
function finds the closest ancestor to the element. You actually need to use.next('.vuQuestionBubble')
.vuQuestionBubble
是一个类vuQuestionBubble
is a classclosest
遍历到祖先元素,因此尝试siblings('vuQuestionBubble')
closest
traverses to ancestor element, so trysiblings('vuQuestionBubble')
进行翻译:最接近的显然是祖先,而不是兄弟姐妹。
参考: http://api.jquery.com/closest/
休息一下,准备一个工具包 -吉。
Translated: Closest apparently gets ancestors, not siblings.
ref: http://api.jquery.com/closest/
Have a break, have a kit-kat.