2014-11-04 56 views
0

//這給了我的背景色IMG黃色,但如果我選擇另一張圖片是無法去除的第一選擇IMG底色顏色:需要有關所選圖像鼠標背景色的幫助。並刪除背景爲其它圖像所選

$('img').mousedown(function() { 
    var $selectedDiv = $(this).parent(); 
    var divId = $selectedDiv.attr('id'); 
    currentCol = divId [1]; 
    currentRow = divId [3]; 
    $("#r"+currentCol+"c"+currentRow).css("background", "Yellow"); 
}); 
+1

請發佈您的HTML的例子。 – 2014-11-04 17:43:48

回答

0

這是我會做什麼:

$('img').mousedown(function() { 
    $('img').css('background', ''); // This will set back the background colour to whatever colour you need image to be. 
    // You can now process the current image. 
    var $selectedDiv = $(this).parent(); 
    var divId = $selectedDiv.attr('id'); 
    currentCol = divId [1]; 
    currentRow = divId [3]; 
    $("#r"+currentCol+"c"+currentRow).css("background", "Yellow"); 
}); 
+0

請注意,如果你只是想刪除一個屬性,'$('img').css('background','');'將刪除這個特殊的'background'屬性。 – 2014-11-04 17:46:36

+0

編輯:) – Nico 2014-11-04 17:47:28

+0

雖然我會用JQuery的'siblings()'方法去。正因如此,OP不會拍得太大。 – 2014-11-04 17:48:47

0

怎麼樣簡單的

$('img').mousedown(function() {....}); 

取代
$('img').mouseover(function() {....}); 

我認爲應該解決您的問題。