简体   繁体   English

jQuery从一个位置提取html代码并将其放置在另一个位置

[英]jQuery Extracting html code from one spot and placing it in another

I'm trying to manipulate a banner slider where it would take all the DOM rendered img tags from all the banners – except the first – and then load them back in one at a time in increments of a few seconds via a set Timeout . 我正在尝试操纵横幅滑块,该滑块将从所有横幅中获取所有DOM呈现的img标签(除了第一个),然后通过设置的Timeout以几秒钟的增量一次将它们加载回一个。 I'm just trying to not bog down the page load because of the amount/size of the images. 由于图像的数量/大小,我只是试图不降低页面加载量。

My issue is that I cannot seem to get the if statement to bypass the first instance of the img tag and remove the others. 我的问题是,我似乎无法获得if语句来绕过img标签的第一个实例并删除其他实例。 Its all or none, so I think the img[0] may not be correct. 全部还是全部,所以我认为img [0]可能不正确。 I have also tried .first() with no luck, and also css' first-child & nth-child(1) . 我也没有运气尝试过.first() ,还有css的first-childnth-child(1) I also tried creating a new array with the img tag html and then continue on...but that proved No Good either. 我也试图创建一个与img标签HTML一个新的数组,然后继续......但事实证明没有不太妙

Any ideas how I can get this going? 有什么想法可以解决这个问题吗? And if y'all have a better approach, I'm open for suggestions. 如果你们都有更好的方法,我欢迎您提出建议。

Thank you. 谢谢。

 var img = $('div > img'); $.each(img, function(i) { if ( !img[0] ) { img.remove(); setTimeout(function(){ $('.main').append(img); }, i * 2000); } }); 
 .main { width: 200px; height: 80px; padding: 10px; overflow: visible; border: 2px solid red; } .div1, .div2, .div3 { width: 200px; height: 80px; background: #ccc; float: left; position: absolute; } .div2 {left:232px;} .div3 {left:437px;} 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="main"> <div class='div1'> <img src="#" alt=""> </div> <div class='div2'> <img src="#" alt=""> </div> <div class='div3'> <img src="#" alt=""> </div> </div> 

It seems like you're talking about lazy loading. 似乎您正在谈论延迟加载。 Have a look at lazysizes , they've got a demo link. 看看lazysizes ,他们有一个演示链接。

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

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