简体   繁体   English

如何将jQuery隐藏/显示按钮与隐藏/显示的div分开?

[英]How to separate jQuery hide/show button from the hidden/shown div?

I would like to use a clickable div in my header to reveal a div surposedly below my main content area, but I can't figure out how to do that. 我想在标题中使用可点击的div,以在我的主要内容区域下方显示一个div,但是我不知道该怎么做。 Right now I'm using a basic jQuery like this: 现在,我正在使用像这样的基本jQuery:

    jQuery(document).ready(function() {
    jQuery(".content").hide();
    jQuery(".expand").click(function()
    {
    jQuery(this).next(".content").slideToggle(500);
    });
});

and a html like this: 和这样的html:

<div class="expand">expand</div>
<div class="content">lorem ipsum</div>

but if I separate the two divs, the function breaks. 但是如果我将两个div分开,功能就会中断。

If you can lead me in the right direction, it will me much appreciated! 如果您能引导我朝正确的方向前进,将不胜感激!

Kind regards 亲切的问候

but if I separate the two divs, the function breaks. 但是如果我将两个div分开,功能就会中断。

Because you're using next() which gets the next element next to $(this) . 因为您使用的是next() ,它获取$(this)旁边的下一个元素。 This will work no matter where .content is: 无论.content在哪里,它都将起作用:

jQuery(document).ready(function () {
    var $content = jQuery(".content"); // Cache
    $content.hide();
    jQuery(".expand").click(function () {
        $content.slideToggle(500);
    });
});

You could use nextAll().first() instead of next() . 您可以使用nextAll().first()代替next()

jQuery(document).ready(function() {
    jQuery(".content").hide();
    jQuery(".expand").click(function() {
        jQuery(this).nextAll(".content").first().slideToggle(500);
    });
});

This way if you have multiple .content on your page, .expand will only expand the next .content on the page. 这样,如果页面上有多个.content.expand将仅扩展页面上的下一个.content

There's a few things you can do here to improve performance and stability. 您可以在这里做一些事情来提高性能和稳定性。 First off, instead of using the next() method, you would just use 首先,您无需使用next()方法,而只需使用

$('.expand').click(function () { $('.content').slideToggle(500); });

Of course, this has the obvious drawback (?) of expanding and contracting all .content areas when you click on any .expand . 当然,这有一个明显的缺点(?),当您单击任何 .expand时,会扩展和收缩所有 .content区域。

But, you can improve your performance and stability if you add a couple of html attributes to the .expand and the .content , like so: 但是,如果在.expand.content添加几个html属性,则可以提高性能和稳定性,如下所示:

<div class="expand" data-for="content_id">Click to toggle</div>
<div class="content" id="content_id">This is the toggable content</div>

First, this requires a recent version of jQuery (ideally 1.6 or greater) for the data- attributes. 首先,这需要data-属性的jQuery最新版本(最好是1.6或更高版本)。 I use a data-for to signify that one particular .expand applies to one particular .content . 我使用data-for ,以表示一个特定的.expand适用于一个特定的.content This is similar to using the for attribute on label and form elements. 这类似于在label和form元素上使用for属性。

Second, the .content areas have a new (and unique to each one) id applied. 其次, .content区域应用了一个新的(并且对每个人来说都是唯一的) id This lets jQuery selectors target those items much quicker through the getElementById() function. 这使jQuery选择器可以通过getElementById()函数更快地定位这些项目。

Finally, the new javascript would look like this: 最后,新的javascript如下所示:

$('.expand').click(function () {
    var id = '#' + $(this).data('for');
    $(id).slideToggle(500); 
});

And if you need accordian functionality (ie, roll up all .content whenever one is clicked, and activate the item that matches the clicked data-for : 并且,如果您需要手风琴功能(即,每当单击一个内容时将所有.content汇总,并激活与所单击的data-for相匹配的项目:

$('.expand').click(function () {
    var id = '#' + $(this).data('for');

    $('.content').slideUp(500);
    $(id).slideDown(500); 
});

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

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