2013-01-03 29 views
0

文字和背景顏色我每四個圓了一個獨特的背景顏色和跨度裏面是白色的。 當用戶點擊在每個圓我想要那個圓切換背景和跨度顏色,或者換句話說,我想要的背景色爲跨度來設定,和背景成爲白。接通點擊

我的代碼做這個正確的,但是當我點擊其他任何圈子我想這個圈子有白色背景和彩色跨度而此前圓回默認(白色跨度,彩色背景)。

jQuery的:

$("#fifthcircleholder li").click(function() { 
    var currentspan = $(this).find("span"); 
    var allspans = $("#fifthcircleholder li").find("span"); 

    $(this).find("span").css({ 
     color: $(this).css("background-color") 
    }); 
    $(allspans).not(currentspan).css({ 
     "color": "#fff" 
    }); 
    $(this).css({ 
     "background-color": "#ffffff" 
    }) 

    var found = $("#fifthcircleholder li"); 
    if (found.css("background-color") == "#fff") { 
     $(this).find("span").css({ 
      "background-color": $(this).css("color") 
     }); 
    } 


}); 

的HTML:

<ul id="fifthcircleholder"> 
    <li id="fifthc1"><span>blah blah</span></li> 
    <li id="fifthc2"><span>blah</span></li> 
    <li id="fifthc3"><span>blah</span></li> 
    <li id="fifthc4"><span>blah</span></li> 
</ul> 
+1

你能不能降低所有的只是給予 「積極」 圈的「活躍「類,並創建CSS規則來影響這個呢? –

+0

@ChristianWattengård嗯,不是真的,項目和他們的顏色數將是動態 –

+0

不需要無關緊要。你可以給你的所有圈子一個班級,說「圈子」。然後你做$(「。circleholder」)。removeClass(「active」); $( 「#fifthcircleholder」)addClass( 「活動」)。然後,它不會不管你有多少circleholders有... –

回答

1

在花了一條縫,希望這將有所幫助:

http://jsfiddle.net/jnLMy/

HTML:

<ul id="fifthcircleholder"> 
<li id="fifthc1"><span>blah blah</span></li> 
<li id="fifthc2"><span>blah</span></li> 
<li id="fifthc3"><span>blah</span></li> 
<li id="fifthc4"><span>blah</span></li> 
</ul>​ 

CSS:

li { width:100px; padding:20px; cursor:pointer; text-align:center; } 
li span { background:#fff; } 

#fifthc1 { background:lime; } 
#fifthc2 { background:yellow; } 
#fifthc3 { background:orange; } 
#fifthc4 { background:blue; } 

JS:

$("#fifthcircleholder li").click(function() { 

    $('#fifthcircleholder li').each(function() { 
     if(hexc($(this).css('background-color')) === '#ffffff'){ 
      $(this).css('background-color', $(this).find('span').css('background-color')); 
      $(this).find('span').css('background-color', '#ffffff'); 
     }   
    });  

    $(this).find('span').css('background-color', $(this).css('background-color')); 
    $(this).css('background-color', '#ffffff'); 
}); 

function hexc(colorval) { 
    var parts = colorval.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/); 
    delete(parts[0]); 
    for (var i = 1; i <= 3; ++i) { 
     parts[i] = parseInt(parts[i]).toString(16); 
     if (parts[i].length == 1) parts[i] = '0' + parts[i]; 
    } 
    return '#' + parts.join(''); 
} 
+0

你搖滾!這就是你所說的完美答案。 非常感謝。 –

0

試試這個腳本:http://jsfiddle.net/YAJma/

$("#fifthcircleholder li").click(function() { 
    $('span').css({"color":"black", "background":"white"}); 
    $('span',this).css({"color":"red", "background":"yellow"}); 
});​ 
+0

,謝謝,我正在尋找一種方式使用變量來切換顏色,請注意,每個圈子都有不同的顏色,同樣進入跨越時,它的活躍。 –

+0

@Sonyflat在哪裏以​​及如何設置圈子的初始顏色? –

+0

@wirey當前顏色來自外部css文件,但後來它們將從cms中檢索。這就是爲什麼我正在尋找一種使用變量切換顏色的方法,所以它可以與動態內容一起使用。 –

0
$("#fifthcircleholder li").click(function() { 
     //reset potentially previously set colors: 
     $('#fifthcircleholder li').each(function(){ 
      //child span's color 
      var spanColor=$('span',this).css('background-color'); 
      if(spanColor!='white'){ 
       $(this).css('background-color',spanColor); 
       $('span',this).css('background-color','white'); 
      } 
     }); 
     //now for the colors of the currently clicked li 
     var liColor=$(this).css('background-color'); 
     $('span').css('background-color',liColor); 
     $(this).css('background-color','white'); 
}); 
+0

謝謝,我確定它是正確的方向,但是當你點擊一個圓時,所有背景都會消失。 這裏是小提琴:http://jsfiddle.net/kBKjZ/1/ –

+0

對不起。修正了對你[這裏](http://jsfiddle.net/kBKjZ/2/) –