开发者

jQuery - re-use CSS value on another element

开发者 https://www.devze.com 2022-12-24 19:06 出处:网络
I have a background colour set on a series of buttons, .button1, .button2., .button3 & .button4. 开发者_如何转开发Each of these buttons has a different background colour set in CSS.

I have a background colour set on a series of buttons, .button1, .button2., .button3 & .button4. 开发者_如何转开发Each of these buttons has a different background colour set in CSS.

I want to use jQuery to detect the background colour of the button when it is clicked and apply that colour to .toolbar.

Is this possible?


You could do:

$('button[class^="button"]').click(function(){
  $('.toolbar').css('background-color', $(this).css('background-color'));
});

This is generic and will automatically detect clicked button rather than writing code for each button having different classes. Also, this code makes sure that it does the stuff only for those buttons whose class names start with button.


$('button').click(function()
{
    $('.toolbar').css('background-color', $(this).css('background-color'));
});
0

精彩评论

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