Is there any way in jquery to copy myself and all child elements.
習慣沉默
習慣沉默 2017-06-05 11:12:54
0
6
749
<ul class="swiper-wrapper">
    <li class="swiper-slide">
        <img src="./images/banner-1.jpg" alt="">
        <span class="slider-text"></span>
    </li>
    <li class="swiper-slide">
        <img src="./images/banner-2.jpg" alt="">
        <span class="slider-text"></span>
    </li>
    <li class="swiper-slide">
        <img src="./images/banner-3.jpg" alt="">
        <span class="slider-text"></span>
    </li>
    <li class="swiper-slide">
        <img src="./images/banner-4.jpg" alt="">
        <span class="slider-text"></span>
    </li>
</ul>

I want to copy the li tag itself including all sub-elements to the front of the first li tag. jquery has a simple way to write it.

var swiperPic = $(".swiper-slide")
var liHtml = swiperPic.eq(swiperPic.length - 1).html()
swiperPic.eq(0).before("<li class="swiper-slide">" +  liHtml +"<li>")

What I think of is to copy all the child elements under the li tag but not the li tag itself.

習慣沉默
習慣沉默

reply all(6)
小葫芦

Try.clone().find(">*")

小葫芦

$(selector).children(selector) is used to return the child elements of each element in the matching element set.

滿天的星座

Can’t just clone the whole thing$('.swiper-slide')

给我你的怀抱
var swiperPic = $(".swiper-slide");
swiperPic.eq(0).before(wiperPic.eq(swiperPic.length - 1).children().clone());
刘奇

For native js, just use element.cloneNode(true)

左手右手慢动作

.clone

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template