jquery - 选择带有悬停的 jquery 子项

标签 jquery css parent-child

我有以下(损坏的)代码:

 $(".old_post").hover(function(){
    $(this > ".post_right_nav").show();

post _ right _ nav 是一个 div(包含其他 div),其中包含一些控件供用户按下。 我只想在用户将鼠标悬停在帖子上时显示这些控件。 如何正确选择每个帖子的子元素?

最佳答案

你可以使用上下文,下面是说:在 this 的上下文中搜索具有 class='post-right-nav' 的元素

$(".old_post").hover(function(){
    $(".post_right_nav", this).show();
...

这会得到所有后代,如果您只想要 child ,您可以执行以下操作

$(".old_post").hover(function(){
    $(this).children(".post_right_nav").show();
...

我找到了一篇快速文章,介绍了 jQuery 选择器中上下文的使用

http://beardscratchers.com/journal/jquery-its-all-about-context

关于jquery - 选择带有悬停的 jquery 子项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1138997/

相关文章:

css - Ionic-v4 中的侧边菜单背景颜色

html - Chrome 浏览器的不透明度

nhibernate - 在 NHibernate 中使用 IList、IEnumerable、ISet 或 ICollection 有什么区别?

javascript - 循环嵌套 json 对象并显示键/值对

javascript - 如何使这个 SimpleModal 成为 onLoad?

javascript - 克隆、播放和替换克隆的 Dom - Javascript/Jquery

python - 如何从带有parent_id的dict创建带有children的dict

java - 集成到 Maven 模块项目中

jQuery Datepicker - 如何将日期格式化为纪元时间戳(以秒而不是毫秒为单位)

javascript - jQuery.inArray 的行为不符合预期