0

我需要做的是在单击 .info 时切换 .information ,同时隐藏所有其他 .information div。我目前可以这样做我的问题是当我单击关闭打开的 .information div 时它不会关闭。如何修改以下代码来做到这一点?我有一个 toggle() 所以认为它会起作用?只是掌握jQuery!

<ol id="resonsToCome">
    <li>
        <p>A packed and extensive activites programe</p>
        <div class="info">
            <img src="images/icons/information.png" width="24" height="24" />
        </div>
        <div class="information">
            <p>The 40+ activities range from 1 to 2 hours. As everything is on site there is no time wasted travelling which ensures up to 5/6 activities per day.</p>
        </div>
    </li>
    <li>
        <p>Instructors stay with their group throughout your stay</p>
        <div class="info">
            <img src="images/icons/information.png" width="24" height="24" />
        </div>
        <div class="information">
            <p>Building a trust and friendship with our customers makes your stay memorable in so many ways.</p>
        </div>
    </li>
    <li>
        <p>Friendly and experienced instructors</p>
        <div class="info">
            <img src="images/icons/information.png" width="24" height="24" />
        </div>
        <div class="information">
            <p>Our instructors take part in a rigorous selection and continuous training programme.</p>
        </div>
    </li>
    <li>
        <p>A great environment and perfect location!</p>
        <div class="info">
            <img src="images/icons/information.png" width="24" height="24" />
        </div>
        <div class="information">
            <p>25 acres of beautiful Somerset fields, woodlands and river only 2 hours from London and 1 hour south of Bristol and Bath.</p>
        </div>
    </li>
</ol>

和 jQuery

$('div.info').on('click', function() {
        $('.information').hide();
        $(this).closest('ol#resonsToCome li').find('.information').toggle();
    });
4

3 回答 3

1

由于您正在隐藏所有内容,因此切换只会重新显示您想要隐藏的项目。试试这个

$(document).ready(function(){
    $('div.info').on('click', function() {
        var closestIsVisible = $(this).closest('ol#resonsToCome li').find('.information').is(":visible");
        $('.information').hide();
        if(!closestIsVisible)
        {
            $(this).closest('ol#resonsToCome li').find('.information').toggle();
        }
    });
});
于 2012-09-25T13:39:09.310 回答
0

试试下面的代码:

$("div.info").on("click", function() {
    var el = $(this).siblings(".information");
    $("#resonsToCome .information").not(el.show()).hide();
});
于 2012-09-25T13:30:22.487 回答
0

看到这个:

$('div.info').click(function(e) {
   $(this).parent().find('.information').toggle();
});

这将起作用。在这里工作小提琴 - http://jsfiddle.net/RCefe/1/

单击 1,2,3,4,因为它们是 div 中具有“信息”类的文本,因为图像不可见。希望这可以帮助。

于 2012-09-25T13:46:34.727 回答