0

我有以下脚本来下拉一个 div。正确的是,当我在 div 外部单击或选择另一个 div 向下时,已经滴下来的 div 正在向上。

但是当我点击 div 中的任何内容时,它不应该上升吗?

Javascript:

$(document).ready(function() {
    $('#favorite_holder').click(function(e) {
        $('#favorite_container').slideToggle("fast");
        $('#account_container').slideUp("fast");
        $('#cart_container').slideUp("fast");
        e.stopPropagation();
    });

    $('#account_holder').click(function(e) {
        $('#account_container').slideToggle("fast");
        $('#favorite_container').slideUp("fast");
        $('#cart_container').slideUp("fast");
        e.stopPropagation();
    }); 

    $('#cart_holder').click(function(e) {
        $('#cart_container').slideToggle("fast");
        $('#favorite_container').slideUp("fast");
        $('#account_container').slideUp("fast");
        e.stopPropagation();
    });

    $(document).click(function(){
        $('#favorite_container, #account_container, #cart_container').slideUp("fast");
    });
});

HTML:

<div id="favorite_holder">
    <span class="price">click here</span>
</div>
<div id="favorite_container" style="display: none;">
    content here...
</div>
<div id="account_holder">
    <span class="price">click here</span>
</div>
<div id="account_container" style="display: none;">
    content here two ...
</div>
<div id="cart_holder">
    <span class="price">click here</span>
</div>
<div id="cart_container" style="display: none;">
    <form>
        First name: <input type="text" name="firstname"><br>
        Last name: <input type="text" name="lastname">
    </form>
</div>

jsfiddle

4

2 回答 2

1

你可以这样做:

$('#favorite_container, #account_container, #cart_container').click(function(e){
    e.stopPropagation();
})

演示小提琴

于 2013-10-16T11:30:03.280 回答
1

使用 html 和 javascript 的简单方法:

html:

<div id="favorite_holder" class="parent">
    <span class="price">click here</span>

<div id="favorite_container"  class="child" style="display: none;">
    content here...
</div>
</div>

<div id="account_holder" class="parent">
    <span class="price">click here</span>

<div id="account_container" class="child" style="display: none;">
    content here two ...
</div>
</div>


<div id="cart_holder" class="parent">
    <span class="price">click here</span>

<div id="cart_container" class="child" style="display: none;">
    <form>
        First name: <input type="text" name="firstname" /> <br\>
        Last name: <input type="text" name="lastname" />
    </form>
</div>
</div>

javascript:

$(document).ready(function() {
    $('.parent').click(function() {
        $('.child').slideUp("fast");            
        $(this).find("div").slideDown("fast");           
    });


    $('.child').click(function(e) {
        e.stopPropagation();
    });   
});
于 2013-10-16T12:07:42.700 回答