javascript - onclick 在下面的 div 中显示颜色

标签 javascript jquery html background

我有一个颜色列表,显示在我页面上的 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>

最佳答案

试试下面的代码:

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

})

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

关于javascript - onclick 在下面的 div 中显示颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15070845/

相关文章:

jquery - 用 jquery 在 </span> 之后添加 <br/>

ruby-on-rails - 包括带有 :cache => true into a rails app 的大型 JS 库

javascript - 使用 javascript 或 jquery 检测 struts2 自动完成器标记值的变化

javascript - Angular : Two-way binding 中的响应式(Reactive)表单

javascript - 在 Vue3-carousel 中显示多个图像

jquery - 输入:hidden selector的用法

javascript - 当我将 JS 版本更改为 3.3.7 时,Bootstrap 选项卡停止工作

javascript - 切换光标以显示我可以放置元素的位置

javascript - 页面加载后加载 iFrame

php - 使用输入字段(类型文件)选择图像后更改文本