简体   繁体   English

使用JavaScript删除HTML块

[英]remove a block HTML with javascript

I use owl-carousel in a project angularjs I try to reinit owl-carousel after data-change with $(element).data('owlCarousel').destroy(); 我在项目angularjs中使用owl-carousel,尝试在数据更改后用$(element).data('owlCarousel').destroy(); owl-carousel $(element).data('owlCarousel').destroy(); and remove this block: 并删除此块:

<div class="owl-wrapper">
<div class="owl-item" ></div>
<div class="owl-item" ></div>
<div class="owl-item" ></div>
<div class="owl-item" ></div>
<div class="owl-item" ></div>
</div>

I try this: 我尝试这样:

 if(typeof $(element).data('owlCarousel') != 'undefined'){
                        $(element).data('owlCarousel').destroy();
                        $(element).find(".owl-item").removeClass("owl-item");                   
                    }

ON page load It's working fine but on data change it's not working for exemple I have 5 items to display then I shoud get 5 block <div class="owl-item" ></div> but I get my 5 items expected and more empty block . 页面加载正常,但在数据更改上不起作用,例如我要显示5个项目,然后我应该获得5个块<div class="owl-item" ></div>但我得到了5个项目的期望值等等空块。

你尝试过使用

$(".owl-item").removeClass("owl-item");

You can remove the whole element like this: 您可以像这样删除整个元素:

 const remove = el => el.parentElement.removeChild(el); const owl = document.querySelector('div.owl-wrapper'); // remove it remove(owl) // check if it's removed (true = no such element in DOM) console.log(document.querySelector('div.owl-wrapper') === null) 
 <div class="owl-wrapper"> <div class="owl-item" >1</div> <div class="owl-item" >2</div> <div class="owl-item" >3</div> <div class="owl-item" >4</div> <div class="owl-item" >5</div> </div> 

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

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