【问题标题】:JQuery onclick firing constantly before the element is clicked在单击元素之前不断触发 JQuery onclick
【发布时间】:2012-10-30 18:47:42
【问题描述】:

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

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

这是我的脚本代码:

$(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,但所有回复都给出了相同的结果。

【问题讨论】:

    标签: jquery twitter-bootstrap


    【解决方案1】:

    这条语句将expandClick的结果赋值为handler,即

    $("#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($));
    });
    

    【讨论】:

    • 或者简单的 $("#expand-button").bind('click', expandClick); 基于 $(document) 表明 $jQuery 全局,并且应该提到这种更改需要发生在 OP 代码中的每个 .bind 上。
    • @KevinB 我什至没有意识到 $ 是 jQuery,让我也建议对此进行改进,很棒
    • 这和@Nelson 的答案相结合就成功了!谢谢大家!
    • 哦,尼尔森删除了他的。好的,感谢@JuanMendes 和@KevinB!
    【解决方案2】:

    不想使用:

    $(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());
    }
    

    【讨论】:

    • 为什么建议将 JS 嵌入到 HTML 中?
    • 是的,它也会有同样的不良行为......我仍然不赞成,你把我们带回到了网络编程的黑暗时代,有很多很好的方法可以解决这个问题。顺便说一句,你的建议不起作用,OP想要按钮展开和折叠
    • 现在它确实......也许......也许不是,它仍然调用collapseClick($)而不是分配is作为处理程序。考虑到已经有一个公认的答案,除非你有更好的答案,否则我会考虑删除你的答案,因为当你得到它的工作时,它几乎和我的一模一样。
    猜你喜欢
    • 1970-01-01
    • 2017-09-10
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    • 1970-01-01
    • 2010-11-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多