使用jQuery选择悬停时父级的同级 [英] Select sibling of parent on hover with jQuery

查看:69
本文介绍了使用jQuery选择悬停时父级的同级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试更改.target的css,该css是.hover的父级的同级.似乎无法使此代码正常工作-我不确定在函数开始时是否需要$(this)或$('.target')...我认为它可能是.target,因为我正在使用.css()更改CSS.

I'm trying to change the css of .target that is the sibling of the parent of .hover. Can't seem to get this code to work - I'm not sure if I need $(this) at the beginning of my function, or $('.target')... I think it might be .target because that is what I'm changing the css of with .css().

<script type="text/javascript">
$(document).ready(function() {
    $('.hover').hover(
        function(){
            $(this).parent().siblings('.target').css('display', 'inline');
        },
        function(){
            $(this).parent().siblings('.target').css('display', 'none');
        }
    );
});
</script>

这是我的预感(这也不起作用):

And here is my hunch (which also doesn't work):

$('.target').parent(this).sibling().css('display', 'inline');

这是html

<div class="target" style="display: none;">
</div>
<div>
    <span class="hover">Hover</span>
</div>

EDIT ----------------- 当spanclass="hover"时,似乎不起作用.

EDIT----------------- It seems that it doesn't work when a span is class="hover".

编辑numero dos -------------------- 看来我有两个很深的父母,需要.parent().parent()谢谢.

EDIT numero dos -------------------- It seems that I had my <span> two parents deep and needed .parent().parent() Thanks.

推荐答案

安迪, 检查一下这个小提琴,您似乎在发布的第一个示例中具有正确的代码.如果您可以发布您的html,我们可能会有更好的时间提供帮助. http://jsfiddle.net/nKtzB/3/

Andy, Check this fiddle out, you seem to have the correct code in the first sample that you posted. If you could post your html, we might have a better time helping out. http://jsfiddle.net/nKtzB/3/

这篇关于使用jQuery选择悬停时父级的同级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆