2012-03-21 54 views
2

我想創建一個帶有可移動動作的切換按鈕,就像它在黑暗的一面顯示它應該是黑暗的,並且如果它顯示爲輕的一面。任何機構都可以幫助你寫出如何寫成 。如何在jQuery中創建切換按鈕

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> 
+1

你到目前爲止做了什麼?顯示一些代碼... – jackJoe 2012-03-21 11:43:57

回答

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'); 
    }  
); 
+0

你能爲這個 – Pawan 2012-03-21 12:17:04

+0

提供html代碼嗎?你可以使用你的html代碼..你只需申報class或id並且必須使用css .. – 2012-03-21 12:21:07

+0

我只是使用第一個代碼,但我不能要看到我把這種方式在HTML中使用

Pawan 2012-03-21 12:23:23

0

我認爲你要切換樣式表單擊按鈕時? On this page是一個很好的教程。