【问题标题】:How to dismiss Bootstrap popover generated with dynamic content when click outside popover area单击弹出框区域外时如何关闭使用动态内容生成的 Bootstrap 弹出框
【发布时间】:2014-05-20 09:23:27
【问题描述】:

当我单击实际弹出框之外的任何位置时,我正在尝试隐藏引导弹出框。我已经尝试了这个线程中的解决方案:

How to dismiss a Twitter Bootstrap popover by clicking outside?

但在我的情况下它似乎不起作用(它只是阻止了任何弹出窗口的生成)。我认为这是因为在我的代码中,生成弹出窗口的链接是从页面中动态添加/删除的,从而与事件侦听器混淆。

这是我的html:

       <div id="item-model">
            <div class="activity-col">
                <div class="form-group col-xs-5">
                    <select name="activity_x" id="activity-x" class="form-control">
                        <option value=""></option>
                        <option value="1">Item 1</option>
                        <option value="2">Item 2</option>
                    </select>
                </div>
            </div>
            <div class="vocab-col">
                <span class="vocab-setting">Random</span>
                <a href="#" class="glyphicon glyphicon-pencil edit-btn" title="" data-original-title="Edit"></a>
            </div>
            <div class="phonics-col">
                <span class="phonics-setting">Selected</span>
                <a href="#" class="glyphicon glyphicon-pencil edit-btn" title="" data-original-title="Edit"></a>
            </div>              
            <a href="#" class="glyphicon glyphicon-remove del-btn" title="" data-original-title="Remove" data-content="<button type='button' class='btn btn-primary'>Remove</button> <br /> <button type='button' class='btn btn-default'>Cancel</button>"></a>
        </div>
        <ol>
            <li>
                <div class="activity-col">
                    <div class="form-group col-xs-5">
                        <select name="activity_0" id="activity-0" class="form-control">
                            <option value=""></option>
                            <option value="1">Item 1</option>
                            <option value="2">Item 2</option>
                        </select>
                    </div>
                </div>
                <div class="vocab-col">
                    <span class="vocab-setting">Random</span>
                    <a href="#" class="glyphicon glyphicon-pencil edit-btn" title="" data-original-title="Edit"></a>
                </div>
                <div class="phonics-col">
                    <span class="phonics-setting">Selected</span>
                    <a href="#" class="glyphicon glyphicon-pencil edit-btn" title="" data-original-title="Edit"></a>
                </div>
            </li>
        </ol>

还有 javascript:

/* popover generation
--------------------*/
$('body').popover({
    html: true,
    placement: 'top',
    selector: '.del-btn',
});

/* make new list item
--------------------*/
$('.lesson.new ol').on('change blur', 'select:last', function() {
    var $last_select = $(this);
    if($last_select[0].selectedIndex !== 0) {
        var $ol = $('.lesson.new ol');
        var $li_content = $('#item-model').clone();
        $ol.append('<li></li>');
        var $last_li = $ol.children('li:last');
        $last_li.css({ opacity: 0 });
        $last_li.append($li_content.html());
        $last_li.animate({opacity : 1 }, { duration: 300 });
    }
});

现在发生的情况是,当您在列表的最后一个选择框中选择一个项目时,它会在底部自动生成一个新的列表项目。当您单击删除按钮 (.del-btn) 时,会弹出一个确认窗口。我想要发生的是,如果您在弹出窗口之外单击,我希望隐藏当前打开的弹出窗口。如果您在弹出窗口仍处于打开状态时单击另一个删除按钮,则第一个弹出窗口应关闭并打开新窗口。

【问题讨论】:

标签: javascript jquery twitter-bootstrap popover


【解决方案1】:

这应该为你做。注释应该解释它是如何工作的。

//register an event handler for the entire body of the document

$('body').click(function (event) {
    //find the element that dispatched the event
    var clicktarget = $(event.target);
    //make sure that the element that dispatched the event was NOT the .del-btn
    if (!clicktarget.hasClass('del-btn')) {
        //if ANY element on the body dispatched the event other than the .del-btn
        //hide any open popovers
        $('.del-btn').popover('hide');
    }
});

【讨论】:

    【解决方案2】:

    刚才我读到了很棒的article 为什么永远不应该使用event.stopPropagation()。值得一读。

    【讨论】:

    • 谢谢!读完那篇文章后,我选择了这个:$(document).on('click', function(event) { if (!$(event.target).closest('#menucontainer').length) { // Hide the menus. } });
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-10
    • 2012-11-09
    • 1970-01-01
    • 1970-01-01
    • 2013-11-10
    • 2013-12-13
    相关资源
    最近更新 更多