在单击元素之前,JQuery onclick不断触发

时间:2012-10-30 18:47:43

标签: jquery twitter-bootstrap

我已经查看了十几个关于使用onclickclickbind("click", ...)on("click", ...)等的问题,还没有找到我的问题我有。

基本上它是一个可扩展的div,在不扩展时会隐藏一些内容。我正在使用Twitter Bootstrap collapse类和数据切换按钮来扩展/折叠内容本身,但我还需要修改容器div的CSS以增加高度,以便在视觉上,框中内容将会延伸以包含它。

这是我的脚本代码:

$(document).ready(function() {
    $("#expand-button").bind('click', expandClick($));
    document.getElementById("#expand-button").bind("click", expandClick($));
});

function expandClick($) {
    $("#outer-container").animate({ "height": "350" }, 500);
    $("#expand-button").html("^");
    $("#expand-button").bind("click", collapseClick($));
};

function collapseClick($) {
    $("#outer-container").animate({ "height": "50" }, 500);
    $("#expand-button").html("V");
    $("#expand-button").bind("click", expandClick($));
}

这个想法很简单,处理程序根据按钮的状态旋转进出。实际发生的是,只要我加载页面,就会立即执行expandClick函数,这会导致我的容器无限循环,无论是否点击都会上下跳动。

有什么想法吗?

另外,我不认为它应该是相关的,但HTML看起来像:

    <div id="outer-container" class="container-fluid subsession-collapsed">
        <div class="row-fluid" style="height: 50px">
            <!-- OTHER STUFF... -->
            <div class="span1" id="4">
                <button id="expand-button" class="btn-success" data-toggle="collapse" data-target="#expandable">V</button>
            </div>
        </div>
        <br /><br />
        <div id="expandable" class="row-fluid collapse">
            <div class="span12" style="padding: 0 20px">
                <!-- CONTENT -->
            </div>
        </div>
    </div>

编辑:

我曾经尝试过找到解决方案的一个SO主题是this one,但所有回复都给出了相同的结果。

2 个答案:

答案 0 :(得分:5)

此语句将expandClick的结果指定为处理程序,即

$("#expand-button").bind('click', expandClick($));

应该是

$("#expand-button").bind('click', function() { expandClick($) });

另一个问题是,您要从expandClickcollapseClick添加更多点击处理程序,但从不删除它们

这就是我要重写你的代码的原因,我不知道为什么你要传递$

$(document).ready(function() {
    // Cache your variables instead of looking them up every time
    var expandButton =  $("#expand-button"),
        outerContainer =  $("#outer-container");

    function expandClick() {
        outerContainer.animate({ "height": "350" }, 500);
        expandButton.html("^");
        // Remove the previous handler
        expandButton.off('click', expandClick );
        // Bind the new handler
        expandButton.bind("click", collapseClick);
    };

    function collapseClick() {
       outerContainer.animate({ "height": "50" }, 500);
       expandButton.html("V");
        // Remove the previous handler
       expandButton.off('click', collapseClick);
        // Bind the new handler
       expandButton.bind("click", expandClick);
    }

    expandButton.bind('click', expandClick);
    // What is this????
    //document.getElementById("#expand-button").bind("click", expandClick($));
});

答案 1 :(得分:-1)

而不是使用:

$(document).ready(function() {
    $("#expand-button").bind('click', expandClick($));
    document.getElementById("#expand-button").bind("click", expandClick());
});

尝试

<button id="expand-button" class="btn-success"
data-toggle="collapse" data-target="#expandable" 
onclick="expandClick($)">V</button>

function expandClick($) {
    $("#outer-container").animate({ "height": "350" }, 500);
    $("#expand-button").html("^");
    $("#expand-button").unbind("click", expandClick());
    $("#expand-button").bind("click", collapseClick());
};

function collapseClick($) {
    $("#outer-container").animate({ "height": "50" }, 500);
    $("#expand-button").html("V");
    $("#expand-button").unbind("click", collapseClick());
    $("#expand-button").bind("click", expandClick());
}