从DOM

时间:2017-01-23 17:36:44

标签: javascript html twitter-bootstrap bootstrap-modal

我会尽量做到尽可能彻底(对不起,长篇大论)。

我正在处理一段代码,以便在用户通过点击触发事件后动态加载我的部分内容。

案例场景是:当用户点击帖子时,它会打开modal并使用链接中的slug显示详细视图,即动态生成的内容。但是,当用户关闭DOM时,我需要从modal删除模式。现在我只能hide但我需要删除它。

这是我的代码。

PHP Laravel HTML标记():

<div class="content-blocks blog hidex">

        <section class="content">
            <div class="block-content">
                <h3 class="block-title">My Blog</h3>
                <div id="post-list" class="col-md-10 col-md-offset-1">

                    {% set posts = blogPosts.posts %}

                    {% for post in posts %}
                    <div class="post">

                        <div class="post-thumbnail">
                            {% if post.featured_images.count %}
                            {% set image = post.featured_images|first %}
                            <a class="open-post" href="{{'blog-post'|page}}">
                                <img
                                        data-src="{{ image.filename }}"
                                        src="{{ image.path }}"
                                        alt="{{ image.description }}"
                                        style="max-width: 100%"/>

                            </a>
                            {% endif %}
                        </div>
                        <div class="post-title">
                            <a class="open-post" href="{{ post.url }}"><h2>{{ post.title }}</h2></a>
                            <p class="post-info">


                                <span class="post-author">Posted by {{ post.user.first_name}} </span>
                                <span class="slash"></span>
                                <span class="post-date">on {{ post.published_at|date('M d, Y') }}</span>
                                <span class="slash"></span>
                                {% if post.categories.count %} in {% endif %}
                                {% for category in post.categories %}
                                <span class="post-category">{{ category.name }}</span>
                                {% if not loop.last %}, {% endif %}
                                {% endfor %}
                            </p>
                        </div>
                        <div class="post-body">
                            <p>{{ post.summary }}</p>
                            <a class="btn open-post" href="{{ post.url }}">Read More</a>
                        </div>
                    </div>
                    {% endfor %}

                    <div class="text-center">
                        {% if posts.lastPage > 1 %}
                        <ul class="pagination">
                            {% if posts.currentPage > 1 %}
                            <li>
                                <a href="{{ this.page.baseFileName|page({ (pageParam): (posts.currentPage-1) }) }}"
                                   aria-label="Previous">
                                    <span aria-hidden="true">&laquo;</span>
                                </a></li>
                            {% endif %}

                            {% for page in 1..posts.lastPage %}
                            <li class="{{ posts.currentPage == page ? 'active' : null }}">
                                <a href="{{ this.page.baseFileName|page({ (pageParam): page }) }}">{{ page }}</a>
                            </li>
                            {% endfor %}

                            {% if posts.lastPage > posts.currentPage %}
                            <li><a href="{{ this.page.baseFileName|page({ (pageParam): (posts.currentPage+1) }) }}"
                                   aria-label="Next">
                                <span aria-hidden="true">&raquo;</span>
                            </a>
                            </li>
                            {% endif %}
                        </ul>
                        {% endif %}
                    </div>
                </div>
            </div>
        </section>
    </div>

JavaScript:

//Blog post Modal
    $('.open-post').on('click', function(){
        var postUrl = $(this).attr("href");

        var post = '<div class="modal" id="post-modal"><div class="inline-menu-container"><a id="modal-close" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span></a></div><div class="modal-dialog"><div class="modal-content"></div></div></div>';

        $(post).modal({
          remote: postUrl
        })

        return false;
    });
// close the modal
   $('#close').on( 'click', function() {
      $('.name-block').removeClass('reverse');
      $('.name-block-container').removeClass('reverse');
      $('.menu-blocks').removeClass('hidex');
      $('.content-blocks').removeClass('showx');
      $('.content-blocks').addClass('hidex');
      $('.inline-menu-container').removeClass('showx');
      $('.inline-menu-container').addClass('hidex');
      $('.menu-item').removeClass('active');
    });

DOM中的Javascript代码结果:

<div class="modal in" id="post-modal" style="display: block; padding-left: 0px;">
    <div class="inline-menu-container">
    <a id="modal-close" class="close" data-dismiss="modal"><span aria-hidden="true">×</span></a>
</div>
<div class="modal-dialog">
   <div class="modal-content">
   /// all the dynamic content in the modal
   </div>
</div>

现在我需要在不刷新页面的情况下删除此结果。 的更新 这是关闭modal后在Inspect中查看HTML的方式: enter image description here

注意: 我尝试使用#post-modal id从DOM中删除模态内容,但未成功。此外,我还没有找到使用Delegate的示例,而在加载'DOM'后,javascript完全添加了模态元素和内容。

感谢。

4 个答案:

答案 0 :(得分:1)

感谢I.G. PascualSerg Chernata,这是解决方案:

Javascript代码:

$(document).on('hidden.bs.modal', '.modal', function () { $("#post-modal").remove(); $(".modal-dialog").remove(); });

答案 1 :(得分:0)

Bind an event然后remove()

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

$('.modal').on('hidden.bs.modal', function () {
    $('.modal').remove();
});

答案 2 :(得分:0)

下面给出的片段将告诉你如何做你想做的事情,而不必做任何额外的事情,甚至不编写传统的bootstrap模态代码。

如需进一步参考,您还可以访问this link。它将向您展示如何以最少的努力动态创建模态。

&#13;
&#13;
function open_modal(name) {

  var message = $('#frm_1');
  BootstrapDialog.show({
    title: 'New message to ' + name,
    message: $('#frm_1'),
    onshown: function() {
      $('#recipient-name').val(name);
    },
    onhide: function(dialog) {
      console.log('Cerrada');
      $('#hidden-div').append(message);
    },
    buttons: [{
      label: 'Close',
      action: function(dialog) {
        dialog.close();
      }
    }, {
      label: 'Send message',
      cssClass: 'btn btn-primary',
      action: function(dialog) {
        // Do whatever send message does, here
      }
    }]
  });



}
&#13;
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap3-dialog/1.34.7/css/bootstrap-dialog.min.css">

<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" integrity="sha384-wvfXpqpZZVQGK6TAh5PVlGOfQNHSoD2xbE+QkPxCAFlNEevoEH3Sl0sibVcOQVnN" crossorigin="anonymous">


<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>

<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap3-dialog/1.34.7/js/bootstrap-dialog.min.js"></script>

<button type="button" class="btn btn-primary" onclick="open_modal('@mdo')">Open modal for @mdo</button>
<button type="button" class="btn btn-primary" onclick="open_modal('@fat')">Open modal for @fat</button>
<button type="button" class="btn btn-primary" onclick="open_modal('@getbootstrap')">Open modal for @getbootstrap</button>


<div id="hidden-div" style="display : none">

  <form id="frm_1">
    <div class="form-group">
      <label for="recipient-name" class="control-label">Recipient:</label>
      <input type="text" class="form-control" id="recipient-name">
    </div>
    <div class="form-group">
      <label for="message-text" class="control-label">Message:</label>
      <textarea class="form-control" id="message-text"></textarea>
    </div>
  </form>
</div>
&#13;
&#13;
&#13;

答案 3 :(得分:0)

您可以尝试

$(modal).modal().on('hidden.bs.modal',function(e){
    e.target.remove();
})

您可以直接在初始化本身上添加隐藏事件。因此您不必每次都分别触发。