简体   繁体   English

获取.show一次仅显示一个div

[英]Get the .show to only show one div at the time

Basically I'm using the jQuery .show to view the sections that are hidden in my articles. 基本上,我使用jQuery .show来查看文章中隐藏的部分。 And this works, but it's global. 这行得通,但这是全球性的。 Which means that every section following a "read more" div is revealed when I press one of them. 这意味着当我按下其中一个时,显示出“更多” div之后的每个部分。

How can I limit it so that after I press any of the "read more" div's only the following element (which in this case is hidden sections containing text) is made visible. 如何限制它,以便在按下任何“更多” div之后,只有以下元素(在这种情况下为包含文本的隐藏部分)才可见。

This is my current script for this feature: 这是该功能的当前脚本:

$('div.showrest').click(function(){

    $(".blog > .invis").show("fast");

});

$('div.showless').click(function(){

    $(".blog > .invis").hide(500);

});

This is my current html structure: 这是我当前的html结构:

<article class="blog">

    <h3> Title </h3>

    <p class="blog"> 

    Short summary here.

    </p>

    <div class="showrest"> Read more </div>

<section class="invis" style="display:none;">

    <p class="blog">  

    Here is the rest of the article.

    </p>

    <div class="showless"> Hide text </div>

</section>

    <footer class="blog">

    Footer text.

    </footer>

</article>

If only I had one article I wanted this feature on it wouldn't be a problem, but I got many and I would like this to be possible on all of them, but individually. 如果只有一篇文章,我希望在此功能上没有问题,但是我有很多,并且我希望在所有文章上都可以做到这一点,但要单独做到。

I know it is possible to do it with multiple different id's, classes and scripts. 我知道可以使用多个不同的ID,类和脚本来实现。 But I would prefer if it was all within the same .click script you see above. 但是,我希望它们都位于您上面看到的相同的.click脚本中。

I've been working to find a solution to the issue all day, but I just can't seem to figure it out. 我一直在努力寻找解决问题的方法,但似乎无法解决。

Something like this? 像这样吗 multiple sections and multiple show/hides? 多个部分和多个显示/隐藏?

$('div.showrest').click(function () {
    $(this).next().show("fast");
});
$('div.showless').click(function () {
    $(this).closest('section').hide(500);
});

DEMO DEMO

You can use the .closest selecter to do this. 您可以使用.closest选择器执行此操作。

$('div.showrest').click(function(){

    $(this).closest(".blog > .invis").show("fast");

});

$('div.showless').click(function(){

        $(this).closest(".blog > .invis").hide(500);

});

Change your jquery code like this: 像这样更改您的jquery代码:

$('div.showrest').click(function(){
    $(this).next().show("fast");
});

$('div.showless').click(function(){
    $(this).parent().hide(500);
});

This way you only trigger the show and hide on the relevant elements. 这样,您仅触发显示并隐藏相关元素。

Use this & parents 使用这个和父母

$('div.showrest').click(function(){
    $(this).parents('article').find('.invis').show('fast');
});

$('div.showless').click(function(){
    $(this).parents('article').find('.invis').hide('fast');
});

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

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