2012-03-21 21 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

答えて

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

+0

のhtmlコードを提供することができますあなたのHTMLコードを使用することができます..あなたはクラスまたはIDを宣言する必要がありますし、CSSを使用する必要があります.. –

+0

私はちょうど最初のコードで動作しますが、私はこのようにhtmlでこの方法を使用しています

Pawan

0

を? On this pageはそのための素敵なチュートリアルです。

0

私は同じものを探していましたが、あなたがブートストラップを使うことができれば、優れた(非公式の)Bootstrap Switch is availableです。

関連する問題