jquery - 如何在 jQuery 中创建切换按钮

标签 jquery css

我想创建一个具有可移动 Action 的切换按钮,如果它显示在黑暗的一面,它应该是黑暗的,如果它显示在明亮的一面。任何人都可以帮忙写这个吗 .

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>

最佳答案

你可以试试这个:- 我在某些地方喜欢这个..

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

关于jquery - 如何在 jQuery 中创建切换按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9803864/

相关文章:

css - 为什么我不应该在 CSS 中使用 ID 选择器?

javascript - 在特定时间间隔后更改 'background-image' 标记中的 'body'

jquery - 编写一个新的 jQuery 插件

javascript - 查看他们的 div 时试图使菜单项变为粉红色

javascript - 使用 css 更改 sweetalert 占位符颜色

javascript - 为 div 创建线性透明渐变

javascript - 输入中只有带点(不是逗号)的浮点值 - jQuery

jquery - 如何使用 CSS(或 jQuery,如有必要)限制输入到 HTML Input 中的字符数?

javascript - 从嵌套对象的属性生成代码字符串

javascript - 使用 jQuery 响应 HTML 元素来更改 DIV 的背景和文本