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

dav*_*ior 8 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>
Run Code Online (Sandbox Code Playgroud)

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();
});
Run Code Online (Sandbox Code Playgroud)

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

the*_*der 17

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

你可以用两种方法做到这一点.

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

    $(document).on('hidden', '.custom-modal', function () {
        $(this).remove();
    });
    
    Run Code Online (Sandbox Code Playgroud)
  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();
    });
    
    Run Code Online (Sandbox Code Playgroud)

选项1是可取的,特别是如果有可能打开多个模态.

  • 找到它:停止将`customModal`包装为jQuery对象.将其作为字符串传递.例如customModal ='<div class = ...'当你把它变成一个jQuery对象时,它在你从DOM中取出并继续接收修正后仍然存在. (2认同)
  • 对于在此登陆的其他人,我必须使用以下命令:`$(document).on('hidden.bs.modal','.custom-modal',function(){$(this).remove();} );` (2认同)