javascript jquery 获取最近的父级

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8168202/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 02:31:11  来源:igfitidea点击:

jquery get nearest parent

javascriptjquerydom

提问by mononym

Possible Duplicate:
nearest ancestor node in jQuery

可能的重复:
jQuery 中最近的祖先节点

html:

html:

<div class="post">
    <h2><a href="#" title="">Pellentesque</a></h2>
    <p><a href="#" title="" class="more">More</a></p>
</div>

jQuery

jQuery

$('.more').bind("hover", function(){
    $(this).parent('.post').hide() ;                      
});

on hover (.more) i want to hide the post, but it does nothing if i use parents() instead it deletes ALL the .posts on the page

在悬停 (.more) 时,我想隐藏帖子,但如果我使用 parents() 则它什么也不做,它会删除页面上的所有 .posts

any help appreciated

任何帮助表示赞赏

回答by Exception

Try

尝试

$('.more').bind('hover',function()
{
    $(this).closest('.post').hide();
});

hereis the working demo with single class.
hereis the demo with multiple classes.

是单个类的工作演示。
是具有多个类的演示。

回答by Francis

Use jQuery.closest : http://api.jquery.com/closest/

使用 jQuery.closest :http: //api.jquery.com/closest/