2016-10-02 87 views
1

我想使刷卡效果改變背景圖片,但它卡住了!如何檢測css背景網址

這是我的代碼:


$('#AAA').swipeleft(function() { 

    var BG = document.getElementById("AAA").style.backgroundImage; 

    if (BG = "url('../Image/002.jpg')") { 

     $('#AAA').css('background-image','url(../Image/003.jpg)'); 
    }; 

    if (BG = "url('../Image/003.jpg')") { 

     $('#AAA').css('background-image','url(../Image/004.jpg)'); 
    }; 
}); 

回答

0

你的條件應該包含==操作符。沒有一個=。你想比較這些值,而不是分配。

0

您忘記了替換圖像中的引號,等於運算符應該是==而不是=。當您將字符串與另一個字符串進行比較時,引號很重要。請檢查他們,你會得到這個工作。下面是開關效應的點擊觸發的例子:

$("#myDiv").click(function(){ 
 
    
 
    var BG = $(this).css("background-image"); 
 
    console.log(BG); 
 
    if(BG == 'url("http://lorempixel.com/400/200/")') { 
 
    $(this).css("background-image", "url('http://lorempixel.com/400/300/')"); 
 
    } else if(BG == 'url("http://lorempixel.com/400/300/")') { 
 
    $(this).css("background-image", "url('http://lorempixel.com/400/200/')"); 
 
    } 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 

 
<div style="background:black url('http://lorempixel.com/400/200/'); width: 200px; height: 200px;" id="myDiv"></div>