如何在jQuery中创建切换按钮

时间:2012-03-21 11:37:46

标签: jquery css

我想创建一个具有可移动动作的切换按钮,就好像它在黑暗的一面显示它应该是黑暗的,如果它显示光的一面。任何人都可以帮助解决这个问题

enter image description here

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="js/jquery-1.7.1.min.js"></script>
<script type="text/javascript">

$(document).ready(function(){
$(".styleswitch").click(function() {
    $('link[rel=stylesheet]').attr('href' , $(this).attr('rel')); 
  });
});
  </script>
<link rel="stylesheet" type="text/css" href="css/dark.css" />
<title>Switch Theme</title>
</head>

<body>
<a href="#" class="styleswitch" rel="css/dark.css">Dark</a>
<a href="#" class=" styleswitch " rel="css/light.css">Light</a>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
<div id="module-a"><a href="#" class="styleswitch" rel="css/dark.css">Dark</a>
<a href="#" class=" styleswitch " rel="css/light.css">Light</a></div>
<div id="module-b">Module1</div>
<div id="module-c">Module1</div>
<div id="module-d">Module1</div>
</body>
</html>

4 个答案:

答案 0 :(得分:2)

你可以试试这个: - 我在某个地方发现了这个......

$j("#button1").live('click',function(){  
    //Fade out form if shown and fade in form selected
    $jtest2.fadeOut("slow", function(){
         $jtest1.fadeIn("slow");  
    });

    //The following is inside the click so I do not get added until the first click 
    //and added after every click so I multiply! 
    //Hence why it takes 2 clicks

    $j('#button1').live('click', function(){
        //change class from light to dark
        $j(this).addClass('dark_button').removeClass('light_button');
    }); //I need to change this class to light if
        // button 2 is selected and change button 2 to dark
});

你也可以试试这个: -

$j("#button1, #button2").live('click',
    function(){  

        //figure out what button was clicked. 
        if(this.id === "button1"){
            var btnA = $j(this);
            var btnB = $j("#button2");
            var divA = $j('#test1');
            var divB = $j('#test2');
        }
        else{
            btnA = $j(this);
            btnB = $j("#button1");
            divA = $j('#test2');
            divB = $j('#test1');
        }

        //make sure it is not already active, no use to show/hide when it is already set
        if(btnA.hasClass('dark_button')){
            return; 
        }

        //see if div is visible, if so hide, than show first div
        if(divB.is(":visible")){        
            divB.fadeOut("slow", function(){
                 divA.fadeIn("slow");
            });
        }
        else{//if already hidden, just show the first div
            divA.fadeIn("slow");            
        }

        //Add and remove classes to the buttons to switch state
        btnA.addClass('dark_button').removeClass('light_button');
        btnB.removeClass('dark_button').addClass('light_button');
    }    
);

答案 1 :(得分:1)

这是您正在寻找的jQuery插件:

答案 2 :(得分:0)

我认为你想在点击按钮时切换样式表? On this page是一个很好的教程。

答案 3 :(得分:0)

我正在寻找同样的东西,如果你可以使用Bootstrap,那就是一个优秀的(非官方的)Bootstrap Switch is available