【问题标题】:how to disable event click outside button when click button javascript单击按钮javascript时如何禁用事件单击外部按钮
【发布时间】:2016-08-04 08:10:28
【问题描述】:

我想在点击按钮时(我使用 js)然后在外部发生事件,例如:

$(document) 点击将被禁用。与单击外部按钮时不同,事件单击按钮将被禁用:

<div class="form-group">
        <label>Default select</label>
        <div id="test" class="btn-group bootstrap-select open" style="width: 200px">
            <button class="btn dropdown-toggle btn-default" type="button" title="Alaska" onclick="test()">
                <span class="filter-option pull-left">Alaska</span>&nbsp;
                <span style="border: 0px !important;" class="caret"></span>
            </button>
            <div class="dropdown-menu open" style="max-height: 210.5px; overflow: hidden; min-height: 122px;">
                <ul class="dropdown-menu inner" style="max-height: 198.5px; overflow-y: auto; min-height: 110px;">                       
                    <li><table class="table table-hover">
                        <thead>
                            <tr>
                                <th>#</th>
                                <th>First Name</th>
                                <th>Last Name</th>
                                <th>Username</th>
                            </tr>
                        </thead>
                        <tbody>
                            <tr>
                                <th scope="row">1</th>
                                <td>Mark</td>
                                <td>Otto</td>
                                <td>@mdo</td>
                            </tr>
                            <tr>
                                <th scope="row">2</th>
                                <td>Jacob</td>
                                <td>Thornton</td>
                                <td>@fat</td>
                            </tr>
                            <tr>
                                <th scope="row">3</th>
                                <td>Larry</td>
                                <td>the Bird</td>
                                <td>@twitter</td>
                            </tr>
                        </tbody>
                    </table></li>
                </ul>
            </div>
        </div>
    </div>

当我点击按钮时。

function test() {
        let x = $("#test");
        x.children().eq(1).show(200);
    }

我想要活动。

 $(document).click(function (e) {
        let x = $("#test");
        // x.children().eq(1).hide(200);
    });

将被禁用。而对面

感谢支持

【问题讨论】:

  • 贴出你试过的代码
  • 你发布代码的努力是什么
  • 谢谢。我附上了我的代码
  • 是 jQuery .off() api.jquery.com/off 你追求什么?
  • 但是当我点击外部按钮时,这个事件必须发生

标签: javascript jquery html


【解决方案1】:

我找到了解决方案。谢谢兴趣。 这是我的代码

 $('body').bind('click', function (e) {
        let x = $("#test");
        if ($(e.target).closest("#ndungs").length == 0) {
            x.children().eq(1).hide(200);
        } 
        e.preventDefault();
        return false;
    });
    $("#test").children().eq(0).bind("click", function (e, el) {
        //debugger;
        let x = $("#test");
        x.children().eq(1).show(200);
        e.preventDefault();
        return false;
    });

【讨论】:

    猜你喜欢
    • 2016-03-20
    • 1970-01-01
    • 2015-03-28
    • 2019-08-15
    • 2012-04-07
    • 1970-01-01
    • 2011-06-16
    相关资源
    最近更新 更多