如何删除通过jQuery插入的bootstrap模式?

时间:2013-05-14 00:59:03

标签: jquery twitter-bootstrap

如果我需要插入自定义Bootstrap模式,我决定想要一个可以使用的脚本。我不想让每个页面底部都有空的静态Bootstrap模态HTML,如果它不会一直被使用的话。

所以,这可能是错误的做法,但这是我的尝试。 我创建了一个变量,它是模态'shell'html。然后,当我单击一个设备项时,它将附加到正文。我有一些内容然后克隆并附加到模态的标题和正文。一切正常。但一旦关闭,我无法移除模态。这与我通过JS插入HTML的事实有关,因为如果Modal shell HTML在我的HTML页面中静态存在,则删除工作正常。

HTML:

<ul>
    <li class="span4 device">
        <div class="inner">
            <h3>Device 4</h3>
            <div class="device-product">
                <img class="device-image" src="img/placeholder-holding-image.png" alt="Holding Image" />
                <a href="#" class="hide">Troubleshoot this item</a>
                <a href="#" class="hide">How to use this product</a>
            </div>
            <div class="device-details">
                <div class="control-group">
                    <label class="control-label">Device Type:</label>
                    <span class="field">Really cool device</span>
                </div>
                <!-- Small amount of hidden additional information -->
                <div class="control-group hide">
                    <label class="control-label">Device ID:</label>
                    <span class="field">123456</span>
                </div>
            </div>
        </div>
    </li>
</ul>

jQuery的:

var customModal = $(
    '<div class="custom-modal modal hide fade" tabindex="-1" role="dialog" aria-hidden="true"> \ 
        <div class="modal-header"><button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button></div> \
        <div class="modal-body"></div> \
        <div class="modal-footer"><button class="btn" data-dismiss="modal">Close</button></div> \
    </div>'
);

$('.device').click(function(){
    $('body').append(customModal);
    $(this).find($('h3')).clone().appendTo('.custom-modal .modal-header');
    $(this).find('.device-product, .device-details').clone().appendTo('.custom-modal .modal-body');
    $('.custom-modal.hide').show();
    $('.custom-modal').modal();
});

 $('.custom-modal').on('hidden', function(){
    $(this).remove();
});

所以真的只是我正在努力的删除()。但是,对于我是否以错误/低效的方式解决这个问题的任何评论总是有助于学习!

1 个答案:

答案 0 :(得分:17)

在将hidden div添加到DOM之前,您尝试绑定.custom-modal事件的事件处理程序,因此事件处理程序永远不会绑定到任何内容。

你可以这两种方式。

  1. 委派hidden事件处理程序,以便文档始终监听源自具有自定义模式类的任何元素的hidden事件:

    $(document).on('hidden', '.custom-modal', function () {
        $(this).remove();
    });
    
  2. 在将模态div添加到DOM后绑定事件处理程序:

    $('.device').click(function(){
        // Push the modal markup into the DOM
        $('body').append(customModal);
    
        // Now that it's in the DOM we can find it and bind an event handler
        $('.custom-modal').on('hidden', function () {
            $(this).remove();
        });
    
        // Continue with other init tasks
        $(this).find('h3').clone().appendTo('.custom-modal .modal-header');
        $(this).find('.device-product, .device-details').clone().appendTo('.custom-modal .modal-body');
        $('.custom-modal.hide').show();
        $('.custom-modal').modal();
    });
    
  3. 选项1是首选,特别是如果有可能打开多个模态。