开发者

how to change BG color of <input type="button" />

I have a web page on which there are several buttons and i want to change it's color when it is selected. There is an example of jQuery, please take a look. I want to do the same but i bit confused.

my HTML page is as :

CSS : .blackButton {   
    background: url("Black_button.jpg");  
    vertical-align: middle; 
    margin-top: 1%开发者_如何学C;   
    margin-bottom: 1%;  
}


<div id="id1">
    <input type="button" class="blackButton"/>  
</div>

<div id="id2">
    <input type="button" class="blackButton"/>
</div>

<div id="id3">
    <input type="button" class="blackButton"/>
</div>      



js : I just tried, but not working
  $(function() {

         $(".blackButton").selectable();
      });


Add this javascript, and a css rule for .selected and you should get the desired result.

$(function(){
    $('input.blackButton').live('click', function(e){
        var t = $(e.target);
        if (!t.hasClass('selected'))
            $('input.selected').toggleClass('selected');
        t.toggleClass('selected');
    });
});

Created a jsfiddle.


Change background-color CSS style on clicking the input. You can do it by toggling CSS class. For example:

$(function(){
    $('.blackButton').click(function(){
        var elem = $(this);
        elem.toggleClass('selected-item-class-name');
    });
});


Even though Zafa is on-point, I will add my own version: http://jsfiddle.net/hobobne/me7wf/

If you tell me your next step, I can add it to the example. In a form and submit? Save some setting via ajax? etc

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜