2017-04-04 54 views
-4

我創建幻燈片,但我需要在點擊nextprev按鈕後加載其他圖片。如何更改jQuery鏈接中的數字?

例如:

<div class="photolist-photo" style="background-image: url('/site/img/100/1.jpg')"> 
<a class="left carousel-control" data-slide="prev" href="#myCarousel" role="button"> 
    <span aria-hidden="true" class="glyphicon glyphicon-chevron-left"></span> 
    <span class="sr-only">Previous</span> 
</a> 
<a class="right carousel-control" data-slide="next" href="#myCarousel" role="button"> 
    <span aria-hidden="true" class="glyphicon glyphicon-chevron-right"></span> 
    <span class="sr-only">Next</span> 
</a> 

我只需要在style="background-image: url('/site/img/100/1.jpg')"改變1.jpg2.jpg點擊next按鈕後。

如何獲得1並更改爲2在url中?

+0

你有兩個以上的圖像或僅有1和2? –

+0

你有什麼嘗試?您將需要使用javascript/JQuery設置整個「樣式」屬性。所以你應該跟蹤當前正在查看的號碼,所以你可以調整它並創建正確的路徑,每個按鈕點擊 – musefan

+0

如果你改變什麼都不會發生。因爲圖片需要頁面加載才能顯示 –

回答

1

start = 0; 
 

 
$(function() { 
 
    $('#next').click(function() { 
 
    var current = start; 
 
    start++; 
 
    changeBackground(current , start); 
 
    }); 
 
    
 
    $('#previous').click(function() { 
 
    if (start != 0) { 
 
     var current = start; 
 
     start--; 
 
     changeBackground(current , start); 
 
    } 
 
    }); 
 
}); 
 

 
function changeBackground(from, to) { 
 
    var link = $('.photolist-photo').css('background-image'); 
 
    link = link.replace(from + '.jpg', to + '.jpg'); 
 
    console.log(link); 
 
    $('.photolist-photo').css('background-image', link); 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="photolist-photo" style="background-image: url('/site/img/100/1.jpg')"> 
 
<a class="left carousel-control" data-slide="prev" href="#myCarousel" role="button"> 
 
    <span aria-hidden="true" class="glyphicon glyphicon-chevron-left"></span> 
 
    <span class="sr-only" id="previous">Previous</span> 
 
</a> 
 
<a class="right carousel-control" data-slide="next" href="#myCarousel" role="button"> 
 
    <span aria-hidden="true" class="glyphicon glyphicon-chevron-right"></span> 
 
    <span class="sr-only" id="next">Next</span> 
 
</a>

+0

不!如何更改爲3和4以及...? – mySun

+0

@mySun更改了代碼,您應該在發佈之前指定此問題,然後 –

+0

謝謝您的幫助:-) – mySun