簡體   English   中英

單擊其他按鈕時如何刪除 jQuery .css()?

[英]How can I remove the jQuery .css() when clicking other button?

在這個 jQuery 開關中,我試圖讓當前單擊的按鈕在單擊另一個按鈕時返回其原始狀態。 也喜歡在頁面加載時首先自動點擊關閉按鈕。 我已經嘗試了很多代碼,但似乎無法讓它工作。

HTML:

<!DOCTYPE HTML>
<html>

<head>

    <title>Toggleswitch</title>
    <link rel="stylesheet" type="text/css" href="main.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <script src='script.js' type='text/javascript'></script>

</head>
<body>

<div class="switchcontainer">   
    <button id="darkmodeon">ON</button>
    <button id="darkmodeoff">OFF</button>
</div>

</body>
</html>

CSS:

body{
    background-color: black;
}

.switchcontainer{
    background-color: white;
    display: flex;
    justify-content: space-between;
    border-radius: 50px;
    width: 125px;
    padding: 5px;

}

button{
    width:50px;
    height: 50px;
    border: none;
    border-radius: 50px;
    background-color: #d8d8d8;
    color: #777777;
    font-family: 'calibri light';
    font-size: 17px;
    font-weight: bold;

}

jQuery:

$(document).ready(function(){
    var darkon = '#darkmodeon';
    var darkoff = '#darkmodeoff';

    $(darkon).click(function(){
        $(this).css({
            "background-color": "#85c452",
            "color": "white",
            "transition": "all 0.2s ease"
        });
    });

    $(darkoff).click(function(){
        $(this).css({
            "background-color": "#85c452",
            "color": "white",
            "transition": "all 0.2s ease"
        });

        $(this).off('click',darkon);

    });

});

您可以使用類和 Jquery 輕松完成。

為您的“開啟”狀態創建一個新類。

.on {
     background-color: #85c452;
     color: white;
     transition: all 0.2s ease;
 }

然后更改您的點擊處理程序以打開和關閉此類,而不是設置特定的 CSS 樣式。

$(document).ready(function(){
    var darkon = '#darkmodeon';
    var darkoff = '#darkmodeoff';

    $(darkon).click(function(){
        $(this).addClass("on");
        $(darkoff).removeClass("on");
    });

    $(darkoff).click(function(){
        $(this).addClass("on");
        $(darkon).removeClass("on");
    });
});

單擊一個按鈕時,您可以通過將該按鈕的 CSS 屬性設置為inherit來關閉另一個按鈕。 這會將屬性重置為其默認值。 您可以使用$('#darkmodeoff')$('#darkmodeon')來定位它們,就像使用$(this)

要將Off按鈕設置為默認選中,您只需在$(document.ready)應用樣式即可。 $('#darkmodeoff')[0].style.backgroundColor我使用了$('#darkmodeoff')[0].style.backgroundColor$('#darkmodeoff')[0].style.color

就我個人而言,我還建議添加cursor: pointer按鈕的cursor: pointer ,使其看起來好像您實際上是在單擊它們,並將outline: nonebutton:hover以刪除默認生成的藍色邊框。 我已將這些添加到代碼段中:)

 $(document).ready(function() { var darkon = '#darkmodeon'; var darkoff = '#darkmodeoff'; // Set the off to clicked by default $('#darkmodeoff')[0].style.backgroundColor = "#85c452"; $('#darkmodeoff')[0].style.color = "white"; $(darkon).click(function() { $(this).css({ "background-color": "#85c452", "color": "white", "transition": "all 0.2s ease" }); $('#darkmodeoff').css({ "background-color": "inherit", "color": "inherit", "transition": "all 0.2s ease" }); }); $(darkoff).click(function() { $(this).css({ "background-color": "#85c452", "color": "white", "transition": "all 0.2s ease" }); $('#darkmodeon').css({ "background-color": "inherit", "color": "inherit", "transition": "all 0.2s ease" }); }); });
 body { background-color: black; } .switchcontainer { background-color: white; display: flex; justify-content: space-between; border-radius: 50px; width: 125px; padding: 5px; } button { width: 50px; height: 50px; border: none; border-radius: 50px; background-color: #d8d8d8; color: #777777; font-family: 'calibri light'; font-size: 17px; font-weight: bold; cursor: pointer; /* ADDED */ } button:focus { outline: none; /* ADDED */ }
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="switchcontainer"> <button id="darkmodeon">ON</button> <button id="darkmodeoff">OFF</button> </div>

希望這有幫助! :)

刪除$(this).off('click',darkon); 而是添加$(darkon).trigger('click'); 到您的darkoff事件處理程序和$(darkoff).trigger('click'); 到你的darkon事件處理程序的開始。

在關閉darkoff事件處理程序之前,添加此}).trigger('click');

我會編輯您的原始代碼,但目前我正在使用手機。

  • 如果要關閉該事件,則必須將處理程序作為參數傳遞給 click 事件。
  • 不是即時添加 CSS,而是使用類來定位狀態,這樣更簡潔。
  • 要調用handler ,在頁面加載時,在綁定處理程序后觸發 click 事件。

 var darkon = '#darkmodeon'; var darkoff = '#darkmodeoff'; // This is how you should be binding your click handler // to the button if you are planning to turn of the event // at a later stage. Since, the `off` method expects the same // function to be passed when you attach the event $(darkon).click(addActiveClass); $(darkoff).click(function(e) { addActiveClass(e); $(darkon).removeClass('on'); $(darkon).off('click', addActiveClass); }); function addActiveClass(e) { var $target = $(e.target); $target.addClass('on'); } $(darkon).click();
 body { background-color: black; } .switchcontainer { background-color: white; display: flex; justify-content: space-between; border-radius: 50px; width: 125px; padding: 5px; } button { width: 50px; height: 50px; border: none; border-radius: 50px; background-color: #d8d8d8; color: #777777; font-family: 'calibri light'; font-size: 17px; font-weight: bold; } .on { background-color: #85c452; transition: all 0.2s ease; }
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="switchcontainer"> <button id="darkmodeon">ON</button> <button id="darkmodeoff">OFF</button> </div>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM