1

我有一个颜色列表,在我的页面上显示在一个 50 像素的方框中。

我想做的是能够单击任何颜色框,然后它将在下面的 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>
4

2 回答 2

2

试试下面的代码:

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

})

jsFiddle 的链接http://jsfiddle.net/ACaX9/

于 2013-02-25T15:54:52.780 回答
1

你可以轻松做到。
在每个 div 中插入一个类,如下所示:

<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]);
}
    });

演示

于 2013-02-25T15:55:04.420 回答