开发者

How to get background color of div?

开发者 https://www.devze.com 2023-03-06 03:49 出处:网络
I have a div I want when us开发者_如何学Pythoner clicks on this div the background color of div should be display in alert box using jquery only ?Try this

I have a div I want when us开发者_如何学Pythoner clicks on this div the background color of div should be display in alert box using jquery only ?


Try this

<script type="text/javascript">
$(document).ready(function(){
  $("div").click(function(){
    var $c=$(this).css("background-color");
    alert($c);
  });
});
</script>


$('div').click(function() {
   alert($(this).css('background-color'));
});

jsFiddle.


You can get the color like this:

$('div#test').css('background-color'); // returns RGB

and display it after click like that:

$('div#test').click(function(){
    alert($(this).css('background-color'));
});

See fiddle: http://jsfiddle.net/xWkDW/1/


#divBack{
  background-color:#00FFCC;
}
<div id="divBack">click to Get my background</div>
$('#divBack').click(function() {
    alert($(this).css('background-color'));
});

I have created bin for you please check this link http://codebins.com/codes/home/4ldqpc1

0

精彩评论

暂无评论...
验证码 换一张
取 消