將字符串按某個字符切割成若干個字符串,並以數組形式返回數組
var s = "abc,abcd,aaa"; ss = s.split(","); // 在每一個逗號(,)處進行分解。 console.log(ss); // ["abc", "abcd", "aaa"]
截取數組中_720替換爲_360:app
function changeLargeImg(self) { imgClear(); $("section").show(); // 大圖 var dataLargeSrc = $(self).attr("data-largesrc"); dataLargeSrc = dataLargeSrc.split(","); var topHtml = "<div class='swiper-wrapper'>"; for(var i in dataLargeSrc){ topHtml+= "<div class='swiper-slide' style='background-image:url("+dataLargeSrc[i]+")'></div>"; } topHtml += "</div>"; $(".gallery-top").append(topHtml); var btn = "<div class='swiper-button-next swiper-button-white'></div><div class='swiper-button-prev swiper-button-white'></div>"; $(".gallery-top").append(btn); // 克隆,小圖 var dataSrc = dataLargeSrc.slice(); var data360Src = []; for (var i in dataSrc) { data360Src.push(dataSrc[i].replace(/_720/, "_360")); //replace找到並替換,並返回新的數組,push放入新的數組中; } var thumbsHtml = "<div class='swiper-wrapper'>"; for(var i in data360Src){ thumbsHtml+= "<div class='swiper-slide' style='background-image:url("+data360Src[i]+")'></div>"; } thumbsHtml += "</div>"; $(".gallery-thumbs").append(thumbsHtml); // swiper圖片動效啓動 var galleryTop = new Swiper('.gallery-top', { nextButton: '.swiper-button-next', prevButton: '.swiper-button-prev', spaceBetween: 10 }); var galleryThumbs = new Swiper('.gallery-thumbs', { spaceBetween: 10, centeredSlides: true, slidesPerView: 'auto', touchRatio: 0.2, slideToClickedSlide: true }); // 雙向控制 galleryTop.params.control = galleryThumbs; galleryThumbs.params.control = galleryTop; }