【问题标题】:On append, count the newly created div and destroy this div when a new div is appended在追加时,计算新创建的 div 并在追加新 div 时销毁此 div
【发布时间】:2014-02-12 08:47:34
【问题描述】:

点击时,会附加一个 div,第二次点击时会创建另一个具有相同内容的 div,使其完全重复。

问:创建新的 div 时如何销毁之前的 div?

$(document).on('click', '.LibSectOpen', function() {
         var LibSect = ($(this).find('.SectionName').html())
         $(this).find('.SectionName').empty()
         $(this).append('<div class="LibraryBooks BooksHolder"><img height="30" width="30" class="LibraryBooksGIF" src="../images/ic_loading.gif"></div>').load(function(e) {

        });

【问题讨论】:

标签: javascript jquery html destroy


【解决方案1】:

试试这个。

HTML

<div id="container"></div>
<button>Add Content</button>

jQuery 代码

$('button').on('click', function(){
        $('#container').empty();
        $('#container').append('<div class="LibraryBooks BooksHolder">this is dynamicaly added text</div>');

});

Demo

【讨论】:

    【解决方案2】:

    您应该先删除旧的 div,然后再添加新的。

    $(document).on('click', '.LibSectOpen', function() {
         var LibSect = ($(this).find('.SectionName').html());
         $(this).find('.SectionName').empty();
         $(this).find('.BooksHolder').remove();
         $(this).append('<div class="LibraryBooks BooksHolder"><img height="30" width="30" class="LibraryBooksGIF" src="../images/ic_loading.gif"></div>');
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-04
      • 1970-01-01
      • 1970-01-01
      • 2016-03-05
      • 1970-01-01
      相关资源
      最近更新 更多