简体   繁体   English

jQuery-单击添加/删除类-多个按钮

[英]jQuery - Click Add/Remove Class - Multiple Buttons

For some reason when I add the second click function it stops working completely. 由于某种原因,当我添加第二个单击功能时,它将完全停止工作。 I was wondering if anybody could help pin point what the issue might be? 我想知道是否有人可以帮助指出问题所在?

What I'm trying to do: 我正在尝试做的是:

The default state is "day" and when "night" is clicked, it removes the day class and adds the night class. 默认状态为“白天”,单击“夜晚”时,它将删除白天班级并添加夜晚班级。 Which changes the background image. 哪个会改变背景图像。 Which works... Sort of. 哪个可行... However, when I add the function for the day button to add the day class and remove the night class is breaks and doesn't work. 但是,当我添加白天按钮的功能以添加白天班级和删除夜晚班级时,会中断并且不起作用。

Here's a fiddle of what I have: http://jsfiddle.net/790hqykq/3/ 这是我所拥有的东西: http : //jsfiddle.net/790hqykq/3/

$(document).ready(function () {

    $('.night').click(function () {
        $('#room').addClass('night');
        $('#room').removeClass('day');
    });

    $('.day').click(function () {
        $('#room').addClass('day');
        $('#room').removeClass('night');
    });

});

Thanks!! 谢谢!!

Edit: Also - Is there any way to fade this class change? 编辑:另外-有什么办法淡化此类变化吗? Similar to fadeIn/fadeOut? 类似于fadeIn / fadeOut吗? Thanks! 谢谢!

jsFiddle Demo

The problem with your fiddle is that the #room element has the class day. 你的小提琴的问题在于#room元素有上课日。 So does the anchor element. 锚元素也是如此。 When the event handler is setup 设置事件处理程序时

$('.day').click(function () {

It is also assigned to the room element, and as a result of that, #room ends up also having the event handler attached to it. 它也被分配给room元素,因此,#room最终还附加了事件处理程序。 This causes day to always be selected as the element's class, even when night is clicked. 这使得即使单击了夜晚,也总是将Day选择为元素的类。

You should consider changing the class name to something like daycolor and nightcolor 您应该考虑将类名称更改为daycolornightcolor

<div id="room" class="daycolor">

and

#room.daycolor {
 background: #00CCFF;
}

The element with ID room has the class day , as one of the elements within it. ID为room的元素具有班级day ,作为其中的元素之一。 When you attach the handler, it's being attached to both elements. 附加处理程序时,它同时附加到两个元素上。

This should solve your problem: 这应该可以解决您的问题:

$(document).ready(function () {
    $('.timeButton.day').click(function () {
        $('#room').addClass('day').removeClass('night');
    });

    $('.timeButton.night').click(function () {
         $('#room').addClass('night').removeClass('day');
    });
});

As per your complement about fading, you can use CSS 3 to achieve this: 根据您对衰落的补充,可以使用CSS 3来实现这一点:

#room {
    -webkit-transition: background 0.5s linear;
    -moz-transition: background 0.5s linear;
    -ms-transition: background 0.5s linear;
    -o-transition: background 0.5s linear;
    transition: background 0.5s linear;
}

Demo 演示版

Change the classnames on your children elements and use that selector for your events. 更改子元素上的类名,并将该选择器用于事件。

jsFiddle jsFiddle

HTML: HTML:

<div id="container">
    <div id="room" class="day"> 
        <a class="timeButton day1">Day</a>
        <a class="timeButton night1">Night</a>
    </div>
</div>

JS: JS:

$(document).ready(function () {

    $('.night1').click(function () {
        $('#room').addClass('night');
        $('#room').removeClass('day');
    });

    $('.day1').click(function () {
        $('#room').addClass('day');
        $('#room').removeClass('night');
    });


});

Style: 样式:

#container {
    width: 300px;
    margin: 0 auto;
}
#container a, #container div {
    float: left;
    display: block;
}
#room {
    width: 300px;
    height: 200px;
    position: relative;
}
#room.day {
    background: #00CCFF;
}
#room.night {
    background: #0000CC;
}
#room .day1 {
    left: 30px;
    border: 1px solid black;
}
#room .night1 {
    right: 30px;
    border: 1px solid black;
}
#room .timeButton {
    position: absolute;
    width: 80px;
    height: 25px;
    top: 30px;
    cursor: pointer;
    text-align: center;
}
#room .timeButton:hover {
    background: #fff;
}

In your script, you reference to ".night" instead ".nightButton". 在脚本中,您引用“ .night”而不是“ .nightButton”。

$('.nightButton').click(function () {
    $('#room').addClass('night');
    $('#room').removeClass('day');
});

$('.dayButton').click(function () {
    $('#room').addClass('day');
    $('#room').removeClass('night');
});

To achieve the transition, you can add this CSS propertie to #room. 要实现过渡,您可以将此CSS属性添加到#room。

-webkit-transition: background 2s; /* For Safari 3.1 to 6.0 */
transition: background 2s;

http://jsfiddle.net/790hqykq/13/ http://jsfiddle.net/790hqykq/13/

you can add css3 for the transitions from day to night. 您可以添加css3进行从白天到晚上的转换。 it wont working in older IE browsers 9 and under but is excellent in all modern browsers. 它无法在旧版IE浏览器9及更低版本中运行,但在所有现代浏览器中均非常出色。 browser support. 浏览器支持。 You can use this generator to make the code faster. 您可以使用此生成器使代码更快。

        #room {
        width: 300px;
        height: 200px;
        position: relative;
       -webkit-transition: background 1000ms ease-in-out;
-moz-transition: background 1000ms ease-in-out;
-ms-transition: background 1000ms ease-in-out;
-o-transition: background 1000ms ease-in-out;
transition: background 1000ms ease-in-out;
    }

Demo jsfiddle 演示jsfiddle

Here is another solution, where I just change the css-style via jquery. 这是另一个解决方案,其中我只是通过jquery更改了CSS样式。

Javascript: Javascript:

$(document).ready(function () {
    $('.day').click(function () {
        $('#room').css("background-color", "#00CCFF");
    });

        $('.night').click(function () {
        $('#room').css("background-color", "#0000CC");
    });
});

Also you need to add a background-color to #room: 另外,您需要为#room添加背景色:

background: #00CCFF;

Fiddle: http://jsfiddle.net/790hqykq/7/ 小提琴: http : //jsfiddle.net/790hqykq/7/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM