Home  >  Q&A  >  body text

Is there any way in jquery to copy myself and all child elements.

<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.

習慣沉默習慣沉默2692 days ago696

reply all(6)I'll reply

  • 高洛峰

    高洛峰2017-06-05 11:14:54

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

    reply
    0
  • 高洛峰

    高洛峰2017-06-05 11:14:54

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

    reply
    0
  • 滿天的星座

    滿天的星座2017-06-05 11:14:54

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

    reply
    0
  • 给我你的怀抱

    给我你的怀抱2017-06-05 11:14:54

    var swiperPic = $(".swiper-slide");
    swiperPic.eq(0).before(wiperPic.eq(swiperPic.length - 1).children().clone());

    reply
    0
  • 怪我咯

    怪我咯2017-06-05 11:14:54

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

    reply
    0
  • ringa_lee

    ringa_lee2017-06-05 11:14:54

    .clone

    reply
    0
  • Cancelreply