jQuery .next()和.closest()不起作用

时间:2012-11-14 13:21:21

标签: jquery

我有这个HTML文档,

<strong><a href='#' class='showMore'>Show More</a></strong>
<div class='more'>This is it</div>

我想在div.more鼠标悬停时显示a.showMore

$("a.showMore").hover(function()
{
    $(this).closest('.more').show();

},function()
{

});

我仍然无法显示更多框。 我错过了什么?

谢谢。

2 个答案:

答案 0 :(得分:2)

您可以使用以下内容:

$("a.showMore").hover(function() {
    $(this).parent().next(".more").toggle();
});

答案 1 :(得分:0)

您的代码无法正常工作的原因是因为您正在使用nearest()遍历DOM,尝试获取祖先:http://api.jquery.com/closest/

。更多是你的标签的兄弟,所以请改用它:

$("a.showMore").hover(function()
{
    $(this).siblings('.more').toggle();
});​

编辑:我在发布之后编辑了标记,这将使用以下标记:

<a href='#' class='showMore'><strong>Show More</strong></a>
<div class='more'>This is it</div>