简体   繁体   English

如何使用jQuery再次删除并重新创建元素?

[英]How to remove and recreate element again with jquery?

I have bootstrap carousel and I have 3 resulotion for my banners (web,tablet and mobil) that's why I show different carousel on web,tablet and mobile. 我有自举轮播,并且我的横幅广告(网络,平板电脑和美孚)有3个重新分配,这就是为什么我在网络,平板电脑和移动设备上显示不同的轮播。 it's okey so far but there is something that I couldn't achieve. 到目前为止还不错,但是我无法实现某些目标。

I want to remove my .web-banner class on tablet and I want to recreate again on web resulotion,I want to remove my .tablet-banner on mobil and I want to recreate again on tablet. 我想在平板电脑上删除我的.web-banner类,并想在重新发行网站时重新创建,我想在mobil上删除我的.tablet-banner ,我想在平板电脑上重新创建。

this is my project demo but I couldn't share on stackoverflow because it's not working properly on stackoverflow which is why I upload on codepen click to see my demo 这是我的项目演示,但是我无法在stackoverflow上共享,因为它在stackoverflow上无法正常工作,这就是为什么我在codepen上载单击以查看我的演示

EDIT the code snippet 编辑代码段

 // Web banner function webSlider() { var sources = $('#mycarousel .item img').map(function() { return $(this).attr('src'); }); $.each(sources, function(index, value) { $(".web-banner").append("<div class='item'><img src='" + value + "'></div>"); $('.web-banner').find('.item:first-child').addClass('active'); }) } // Tablet banner function tabletSlider() { var sources = $('#mycarousel .item img').map(function() { return $(this).data('tablet-src'); }); $.each(sources, function(index, value) { $(".tablet-banner").append("<div class='item'><img src='" + value + "'></div>"); $('.tablet-banner').find('.item:first-child').addClass('active'); }) }; // Mobile banner function mobileSlider() { var sources = $('#mycarousel .item img').map(function() { return $(this).data('mobile-src'); }); $.each(sources, function(index, value) { $(".mobile-banner").append("<div class='item'><img src='" + value + "'></div>"); $('.mobile-banner').find('.item:first-child').addClass('active'); }) }; function sliderControl() { var vn = $(window).width(); var large = 1024; var tablet = 767; var mobil = 480; } $(window).on('resize', sliderControl) $(document).ready(function() { webSlider(); tabletSlider(); mobileSlider(); $(".main-role-banner").remove(); }); 
 #mycarousel { height: 400px; } .carousel-inner>.item>a>img, .carousel-inner>.item>img, .img-responsive, .thumbnail a>img, .thumbnail>img { height: 400px; width: 100%; } .divs { width: 300px; margin: 20px; } 
 <link rel='stylesheet prefetch' href='https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css'> <div class="container"> <div id="mycarousel" class="carousel slide" data-ride="carousel"> <!-- Indicators --> <ol class="carousel-indicators"> <li data-target="#mycarousel" data-slide-to="0" class="active"></li> <li data-target="#mycarousel" data-slide-to="1"></li> <li data-target="#mycarousel" data-slide-to="2"></li> <li data-target="#mycarousel" data-slide-to="3"></li> <li data-target="#mycarousel" data-slide-to="4"></li> </ol> <!-- Wrapper for slides --> <div class="carousel-inner main-role-banner" role="listbox"> <div class="item active"> <img src="https://unsplash.it/1000/300?image=68" data-tablet-src=" https://unsplash.it/1000/300?image=21" data-mobile-src="https://unsplash.it/500/600?image=1"> </div> <div class="item"> <img src="https://unsplash.it/1000/300?image=43" data-tablet-src="https://unsplash.it/1000/300?image=1001" data-mobile-src="https://unsplash.it/500/600?image=2"> </div> <div class="item"> <img src="https://unsplash.it/1000/300?image=67"> </div> <div class="item"> <img src="https://unsplash.it/1000/300?image=47" data-tablet-src="https://unsplash.it/1000/300?image=1005"> </div> <div class="item"> <img src="https://unsplash.it/1000/300?image=72" data-mobile-src="https://unsplash.it/500/600?image=3"> </div> <div class="item"> <img src="https://unsplash.it/1000/300?image=80" data-tablet-src="https://unsplash.it/1000/300?image=84"> </div> </div> <div class="carousel-inner web-banner" role="listbox"></div> <div class="carousel-inner tablet-banner" role="listbox"></div> <div class="carousel-inner mobile-banner" role="listbox"></div> <!-- Controls --> <a class="left carousel-control" href="#mycarousel" role="button" data-slide="prev"> <span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span> <span class="sr-only">Previous</span> </a> <a class="right carousel-control" href="#mycarousel" role="button" data-slide="next"> <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span> <span class="sr-only">Next</span> </a> </div> </div> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src='https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js'></script> 

I think this is what you are looking for .empty() . 我认为这就是您正在寻找的.empty() It deletes all the content inside the element. 它将删除元素内的所有内容。

So you need to add these in your function to remove the div 因此,您需要在函数中添加这些div以删除div

 // Web banner function webSlider() { var sources = $('#mycarousel .item img').map(function() { return $(this).attr('src'); }); $.each(sources, function(index, value) { $(".web-banner").append("<div class='item'><img src='" + value + "'></div>"); $('.web-banner').find('.item:first-child').addClass('active'); }) $('.tablet-banner').empty(); $('.mobile-banner').empty(); } // Tablet banner function tabletSlider() { var sources = $('#mycarousel .item img').map(function() { return $(this).data('tablet-src'); }); $.each(sources, function(index, value) { $(".tablet-banner").append("<div class='item'><img src='" + value + "'></div>"); $('.tablet-banner').find('.item:first-child').addClass('active'); }) $('.web-banner').empty(); $('.mobile-banner').empty(); }; // Mobile banner function mobileSlider() { var sources = $('#mycarousel .item img').map(function() { return $(this).data('mobile-src'); }); $.each(sources, function(index, value) { $(".mobile-banner").append("<div class='item'><img src='" + value + "'></div>"); $('.mobile-banner').find('.item:first-child').addClass('active'); }) $('.web-banner').empty(); $('.tablet-banner').empty(); }; function sliderControl() { var vn = $(window).width(); var large = 1024; var tablet = 767; var mobil = 480; } $(window).on('resize', sliderControl) $(document).ready(function() { webSlider(); tabletSlider(); mobileSlider(); $(".main-role-banner").remove(); }); 
 #mycarousel { height: 400px; } .carousel-inner>.item>a>img, .carousel-inner>.item>img, .img-responsive, .thumbnail a>img, .thumbnail>img { height: 400px; width: 100%; } .divs { width: 300px; margin: 20px; } 
 <link rel='stylesheet prefetch' href='https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css'> <div class="container"> <div id="mycarousel" class="carousel slide" data-ride="carousel"> <!-- Indicators --> <ol class="carousel-indicators"> <li data-target="#mycarousel" data-slide-to="0" class="active"></li> <li data-target="#mycarousel" data-slide-to="1"></li> <li data-target="#mycarousel" data-slide-to="2"></li> <li data-target="#mycarousel" data-slide-to="3"></li> <li data-target="#mycarousel" data-slide-to="4"></li> </ol> <!-- Wrapper for slides --> <div class="carousel-inner main-role-banner" role="listbox"> <div class="item active"> <img src="https://unsplash.it/1000/300?image=68" data-tablet-src=" https://unsplash.it/1000/300?image=21" data-mobile-src="https://unsplash.it/500/600?image=1"> </div> <div class="item"> <img src="https://unsplash.it/1000/300?image=43" data-tablet-src="https://unsplash.it/1000/300?image=1001" data-mobile-src="https://unsplash.it/500/600?image=2"> </div> <div class="item"> <img src="https://unsplash.it/1000/300?image=67"> </div> <div class="item"> <img src="https://unsplash.it/1000/300?image=47" data-tablet-src="https://unsplash.it/1000/300?image=1005"> </div> <div class="item"> <img src="https://unsplash.it/1000/300?image=72" data-mobile-src="https://unsplash.it/500/600?image=3"> </div> <div class="item"> <img src="https://unsplash.it/1000/300?image=80" data-tablet-src="https://unsplash.it/1000/300?image=84"> </div> </div> <div class="carousel-inner web-banner" role="listbox"></div> <div class="carousel-inner tablet-banner" role="listbox"></div> <div class="carousel-inner mobile-banner" role="listbox"></div> <!-- Controls --> <a class="left carousel-control" href="#mycarousel" role="button" data-slide="prev"> <span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span> <span class="sr-only">Previous</span> </a> <a class="right carousel-control" href="#mycarousel" role="button" data-slide="next"> <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span> <span class="sr-only">Next</span> </a> </div> </div> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src='https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js'></script> 

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

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