简体   繁体   English

jQuery not:(this)排除类中的所有内容,但上一个函数中调用的除外

[英]jQuery not: (this) exclude everything from a class except for the one called in a previous function

Hey all, I've got some hidden divs that are shown via a dynamic slideToggle function. 嘿,我有一些隐藏的div,它们是通过动态slideToggle函数显示的。 Everything works great, but the divs are left shown when you click on other links. 一切正常,但是当您单击其他链接时,将显示div。 I'd like one div shown at a time. 我想要一次显示一个div。 Clicking hides the others and shows the new one. 单击隐藏其他并显示新的。 It seems I can never get the full function correct! 看来我永远无法获得完整的功能!

Thank you for your time! 感谢您的时间! I appreciate it immensely. 我非常感谢。 Just a 'lil baby here. 这里只是个“小宝贝”。

HTML: HTML:

    <div id="nav">
        <div class="menu"><a id="show_work">WORK</a></div>
            <div id="work" class="sub">
                <p>hidden content</p>
            </div>
        <div class="menu"><a id="show_biography">BIO</a></div>
            <div id="biography" class="sub" >
                <p>hidden content</p>
            </div>
        <div class="menu"><a id="show_contact">CONTACT</a></div>
            <div id="contact" class="sub">
                <p>hidden content</p>
            </div>
     </div>

Javascript that works but leaves them all open: 可以运行的Javascript,但都将其保持打开状态:

$('#work, #biography, #contact').hide();
$('#nav .menu').click(function() {
    $(this).next().slideToggle(400);
    return false;
}).next().hide();

Mangled conceptual Javascript of what I'd like: 我想要的杂乱概念Javascript:

$('#work, #biography, #contact').hide();
$('#nav .menu').click(function() {
    $(this).slideToggle(400);
    $('.sub not:(this)').slideUp(400);
});
return false;
});.next().hide();

I'd just write a function that hides all of the divs on any click: 我只要编写一个函数即可在单击时隐藏所有div:

function hideDivs() {
  $(".menu div").hide();
}

Then your clicks look like this: 然后您的点击如下所示:

$('#show_work').click(function() {
    hideDivs();
    $('#work').slideToggle(400);
});

$('#show_biography').click(function() {
    hideDivs();
    $('#biography').slideToggle(400);
});

$('#show_contact').click(function() {
    hideDivs();
    $('#contact').slideToggle(400);
});

This works by hiding all of the divs no matter what link was clicked. 无论单击什么链接,此操作都可以隐藏所有div。 You could hide the non-target divs manually (shown below), but I think hiding them all is a good approach. 您可以手动隐藏非目标div(如下所示),但是我认为将它们全部隐藏是一种好方法。

Alternative, (uglier and less maintainable - also needs logic to skip the slideToggle call if the "show div" is already visible): 替代方法(更丑陋,更难以维护-如果“ show div”已经可见,也需要逻辑来跳过slideToggle调用):

$('#show_work').click(function() {
    $('#show_biography, #show_contact').hide();
    $('#work').slideToggle(400);
});

$('#show_biography').click(function() {
    $('#show_work, #show_contact').hide();
    $('#biography').slideToggle(400);
});

$('#show_contact').click(function() {
    $('#show_work, #show_biography').hide();
    $('#contact').slideToggle(400);
});

Is this what you're looking for? 这是您要找的东西吗?

$('.sub').hide();
$('#nav .menu').click(function() {
    $('.sub:visible').not($(this).next()).slideUp(400);
    $(this).next().slideToggle(400);
    return false;
});

Here's a fiddle to watch it work. 这里有一个小提琴看它的工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM