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
精彩评论