How to make a radio button look like a toggle button

前端 未结 9 1956
鱼传尺愫
鱼传尺愫 2020-12-02 05:04

I want a group of radio buttons to look like a group of toggle buttons (but still function like radio buttons). It\'s not necessary that they look exactly like toggle button

9条回答
  •  广开言路
    2020-12-02 05:28

    Here is the solution that works for all browsers (also IE7 and IE8; didn't check for IE6):

    http://jsfiddle.net/RkvAP/230/

    HTML

    JS

    $('label').click(function(){
        $(this).children('span').addClass('input-checked');
        $(this).parent('.toggle').siblings('.toggle').children('label').children('span').removeClass('input-checked');
    });
    

    CSS

    body {
        font-family:sans-serif;
    }
    
    .toggle {
        margin:4px;
        background-color:#EFEFEF;
        border-radius:4px;
        border:1px solid #D0D0D0;
        overflow:auto;
        float:left;
    }
    
    .toggle label {
        float:left;
        width:2.0em;
    }
    
    .toggle label span {
        text-align:center;
        padding:3px 0px;
        display:block;
        cursor: pointer;
    }
    
    .toggle label input {
        position:absolute;
        top:-20px;
    }
    
    .toggle .input-checked /*, .bounds input:checked + span works for firefox and ie9 but breaks js for ie8(ONLY) */ {
        background-color:#404040;
        color:#F7F7F7;
    }
    

    Makes use of minimal JS (jQuery, two lines).

提交回复
热议问题