2013-02-25 52 views
1

我有一個顏色列表,在我的頁面上顯示一個50px方框。Onclick在下面的div中顯示顏色

我想要做的是能夠點擊任何顏色框,然後它會在下面的div中顯示點擊的顏色。

<div style="background:#000000; width:50px; height:50px; float: left; margin:4px"></div> 
<div style="background:#c9c9c9; width:50px; height:50px; float: left; margin:4px"></div> 
<div style="background:#737373; width:50px; height:50px; float: left; margin:4px"></div> 
<div style="background:#424242; width:50px; height:50px; float: left; margin:4px"></div> 
<div style="background:#184880; width:50px; height:50px; float: left; margin:4px"></div> 
<div style="background:#3485bf; width:50px; height:50px; float: left; margin:4px"></div> 
<div style="background:#9ad8e6; width:50px; height:50px; float: left; margin:4px"></div> 
<div style="background:#006891; width:50px; height:50px; float: left; margin:4px"></div> 
<div style="background:#0f6769; width:50px; height:50px; float: left; margin:4px"></div> 
<div style="background:#009687; width:50px; height:50px; float: left; margin:4px"></div> 
<div style="background:#79c7c2; width:50px; height:50px; float: left; margin:4px"></div> 
<div style="background:#72b88a; width:50px; height:50px; float: left; margin:4px"></div> 
<div style="background:#669100; width:50px; height:50px; float: left; margin:4px"></div> 
<div style="background:#b5cf8c; width:50px; height:50px; float: left; margin:4px"></div> 
<div style="background:#74750e; width:50px; height:50px; float: left; margin:4px"></div> 
<div style="background:#c7c25f; width:50px; height:50px; float: left; margin:4px"></div> 
<div style="background:#faeb69; width:50px; height:50px; float: left; margin:4px"></div> 
<div style="background:#ffffa8; width:50px; height:50px; float: left; margin:4px"></div> 
<div style="background:#ffc824; width:50px; height:50px; float: left; margin:4px"></div> 
<div style="background:#f5652c; width:50px; height:50px; float: left; margin:4px"></div> 
<div style="background:#f0ab5d; width:50px; height:50px; float: left; margin:4px"></div> 
<div style="background:#e36b10; width:50px; height:50px; float: left; margin:4px"></div> 
<div style="background:#bd3a0f; width:50px; height:50px; float: left; margin:4px"></div> 
<div style="background:#996751; width:50px; height:50px; float: left; margin:4px"></div> 
<div style="background:#78502a; width:50px; height:50px; float: left; margin:4px"></div> 
<div style="background:#c2ae97; width:50px; height:50px; float: left; margin:4px"></div> 
<div style="background:#f4f4f4; width:50px; height:50px; float: left; margin:4px"></div> 
<div style="background:#a31d20; width:50px; height:50px; float: left; margin:4px"></div> 
<div style="background:#d4536c; width:50px; height:50px; float: left; margin:4px"></div> 
<div style="background:#e0d1dc; width:50px; height:50px; float: left; margin:4px"></div> 
<div style="background:#513d7a; width:50px; height:50px; float: left; margin:4px"></div> 
<div style="background:#9178ad; width:50px; height:50px; float: left; margin:4px"></div> 
<div style="background:#d6cee0; width:50px; height:50px; float: left; margin:4px"></div> 

然後當點擊其中一種顏色就會顯示以下

<div id="result"></div> 
+0

我不能看到jQuery的標籤,-1大家誰使用它。 – Toping 2013-02-25 16:23:53

回答

2

試試下面的代碼:

$("div[style]").click(function(){ 
    $("#result").css("background-color",$(this).css("background-color")); 

}) 

鏈接的jsfiddle http://jsfiddle.net/ACaX9/

1

您可以輕鬆完成。
在這樣每個格插入類:

<div class="clickme" style="background:#000000; width:50px; height:50px; float: left; margin:4px"></div> 

和jQuery中,你可以做這樣的事情:

$(document).ready(){ 
     $('.clickme').click(function(){ 
     $('#result').html(rgb2hex($(this).css('background-color'))); 
    }); 

function rgb2hex(rgb) { 
    rgb = rgb.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/); 
    function hex(x) { 
     return ("0" + parseInt(x).toString(16)).slice(-2); 
    } 
    return "#" + hex(rgb[1]) + hex(rgb[2]) + hex(rgb[3]); 
} 
    }); 

DEMO