简体   繁体   English

如何重置jQuery生成的div的内容?

[英]How can I reset the contents of a div generated by jQuery?

I am using the Onsen UI framework with jQuery and my HTML is like this in its initial state?: 我正在使用带有jQuery的Onsen UI框架,而我的HTML处于初始状态是这样的:

<ons-scroller id="category-list">      
</ons-scroller> 

Then I have a function that populates this div: 然后,我有一个函数填充此div:

function createElement(elementId,elementvalue)
{
   var content = document.getElementById(elementId);
   content.innerHTML=elementvalue;
   ons.compile(content);
}

which is then called in the code as: 然后在代码中将其称为:

if (data.has_menu_category==2){
        var htm='';
        htm+='<ons-list>';
        $.each( data.menu_category, function( key, val ) {            
             htm+='<ons-list-item modifier="tappable" class="row" onclick="loadmenu('+
             val.cat_id+','+val.merchant_id+');">'+val.category_name+' <span style="float:right;">'+val.items_count+'</span></ons-list-item>';
        }); 
        htm+='</ons-list>';
        createElement('category-list',htm); 
}

The category-list is then populated with items. 然后在category-list中填充项目。 I cannot figure out however how to RESET that list back to initial state. 但是,我不知道如何将列表RESET回初始状态。 Basically back to 基本上回到

<ons-scroller id="category-list">      
</ons-scroller> 

How can I do that? 我怎样才能做到这一点?

You can simply set the html to an empty string. 您可以简单地将html设置为空字符串。

jQuery way: jQuery方式:

$('#category-list').html('');

core javascript way: 核心JavaScript方式:

document.getElementById('category-list').innerHTML = '';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM