javascript - jquery有什么方法可以復制自己和所有子元素。
問題描述
<ul class='swiper-wrapper'> <li class='swiper-slide'><img src='http://m.4tl426be.cn/wenda/images/banner-1.jpg' alt=''><span class='slider-text'></span> </li> <li class='swiper-slide'><img src='http://m.4tl426be.cn/wenda/images/banner-2.jpg' alt=''><span class='slider-text'></span> </li> <li class='swiper-slide'><img src='http://m.4tl426be.cn/wenda/images/banner-3.jpg' alt=''><span class='slider-text'></span> </li> <li class='swiper-slide'><img src='http://m.4tl426be.cn/wenda/images/banner-4.jpg' alt=''><span class='slider-text'></span> </li></ul>
我想復制li標簽本身包括所有子元素到第一個li標簽前面,jquery有簡單的寫法。
var swiperPic = $('.swiper-slide')var liHtml = swiperPic.eq(swiperPic.length - 1).html()swiperPic.eq(0).before('<li class='swiper-slide'>' + liHtml +'<li>')
我想到的是能復制li標簽下所有子元素但是不包括li標簽本身。
問題解答
回答1:試試.clone().find('>*')
回答2:$(selector).children(selector)用于返回匹配元素集合中每個元素的子元素。
回答3:直接clone整個$(’.swiper-slide’)不就行了么
回答4:var swiperPic = $('.swiper-slide');swiperPic.eq(0).before(wiperPic.eq(swiperPic.length - 1).children().clone());回答5:
原生的js用element.cloneNode(true)就可以了
回答6:.clone
相關文章:
1. bootstrp是col-md-12列的,只有col-md-10有內容,可以讓沒有內容的不占據位置嗎;2. wordpress里,這樣的目錄列表是屬于小工具還是啥?3. 常量在外面不加引號會報錯。4. mysql federated引擎無法開啟5. 百度地圖 - Android app中準備接入地圖sdk,百度VS高德哪個好一點?6. 我的怎么不顯示啊,話說有沒有QQ群什么的7. sublime text3安裝package control失敗8. python 3.4 error: Microsoft Visual C++ 10.0 is required9. android - Genymotion 微信閃退 not find plugin.location_google.GoogleProxyUI10. MySQL 使用 group by 之后然后 IFNULL(COUNT(*),0) 為什么還是會獲得 null
