简体   繁体   English

jquery + Simple Toggle Script +在一个站点上使用多次

[英]jquery+Simple Toggle Script + Use more than once on one site

I have modified this script a little bit to my needs, but now i have come across a problem, which I cant find a solution for. 我已经根据我的需要修改了这个脚本 ,但现在我遇到了一个问题,我无法找到解决方案。

HTML: HTML:

<div  class="toggle">

<p>Grumpy wizards make toxic brew for the evil Queen and Jack.</p>

</div><!-- end .toggle -->

<div class="readMoreDiv">    </div><!-- end .readMoreDiv -->


<div  class="toggle">

<p>Grumpy wizards make toxic brew for the evil Queen and Jack.</p>

</div><!-- end .toggle -->

<div class="readMoreDiv">    </div><!-- end .readMoreDiv -->

Script: 脚本:

$(document).ready(function() {


    // Andy Langton's show/hide/mini-accordion - updated 23/11/2009
    // Latest version @ http://andylangton.co.uk/jquery-show-hide
    var showText='down';
    var hideText='up';

    // initialise the visibility check
    var is_visible = false;

    // insert Show/hide links in the readMoreDiv
    $('.toggle').next('.readMoreDiv').html('<a href="#" class="toggleLink">'+showText+'</a>');

    // hide all of the elements with a class of 'toggle'
    $('.toggle').hide();

    // capture clicks on the toggle links
    $('a.toggleLink').click(function() {

        // switch visibility
        is_visible = !is_visible;

        // change the link depending on whether the element is shown or hidden
        $(this).html( (!is_visible) ? showText : hideText);

        // toggle the display
        $(this).parent().prev('.toggle').slideToggle();
        return false;
    });


});

See it in action here: http://jsfiddle.net/CeBEh/ 在这里看到它: http//jsfiddle.net/CeBEh/

As you can see on Fiddle, the script works good when you are opening and closing just one div. 正如您在Fiddle上看到的那样,当您打开和关闭一个div时,脚本运行良好。 But as soon you start opening and closing the second div, WHILE the first one is still open, the problems begin.... 但是,一旦你开始打开和关闭第二个div,当第一个div仍然打开时,问题就开始......

I would just like to have it work at all times, no matter if no or all divs are currently open. 无论是否所有div都是开放的,我都希望它能随时工作。

Thanks! 谢谢!

Get rid of the is_visible flag and change the code in the click function to this: 摆脱is_visible标志并将click函数中的代码更改为:

var toggleDiv = $(this).parent().prev('.toggle');
// change the link depending on whether the element is shown or hidden
$(this).html(toggleDiv.is(":visible") ? showText : hideText);

// toggle the display
toggleDiv.slideToggle();

http://jsfiddle.net/CeBEh/3/ http://jsfiddle.net/CeBEh/3/

The problem is your is_visible variable which you use in both your callbacks. 问题是您在回调中使用的is_visible变量。 Any a.toggleLink will change that value. 任何a.toggleLink都会改变该值。 Try using an extra class to identify if the div is visible or not, or something else. 尝试使用额外的类来识别div是否可见,或其他东西。

http://jsfiddle.net/CeBEh/1/ http://jsfiddle.net/CeBEh/1/

Remove is_visible and check the actual text of the clicked link. 删除is_visible并检查所单击链接的实际文本。

What if you do it this way instead: 如果你这样做会怎么样:

Html: HTML:

    <div>
        <p class='toggleMe'> Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. </p>
        <a class="toggle">
            Hide box
        </a>
    </div>
    <div>
        <p> Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. </p>
        <a class="toggle">
            Hide box
        </a>
    </div>

javascript: JavaScript的:

    $(document).ready(function(){
          $('.toggle').click(function(){
                $(this).parent().find('p').toggle();
                $(this).text($(this).text() == 'Show box' ? 'Hide box' : 'Show box');
            });
        });

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

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