简体   繁体   English

jQuery切换多个类

[英]Jquery toggle multiple class

My aim is to acheive something like this http://corkboard.me/GlJTVkD34A , when the button that changes the background is clicked both the background of the note and the image in the button are changed. 我的目的是实现类似http://corkboard.me/GlJTVkD34A之类的东西,当单击更改背景的按钮时,便笺的背景和按钮中的图像都被更改。 At the momemt i've been able to get the exact same thing, the onclick event on the note, the div that pops up on top but i tried to change the background by putting all the classes in an array like below and it hasn't worked. 在妈妈那里,我已经得到了完全相同的东西,便笺上的onclick事件,在顶部弹出的div,但我试图通过将所有类放入下面的数组中来更改背景,并且它没有没有工作。 I still have no idea how to make the button image change simultaneously with the note's background. 我仍然不知道如何使按钮图像与便笺的背景同时更改。

//nbg is the button above that is to be cliked to change the backgound
var nbg = $('#nbg');
var count = 0;
nbg.each(function() {
    var thisnotice = $(this);
    thisnotice.click(function() {
    var notice_classes =['changeablenbg_yellow','changeablenbg_green','changeablenbg_pink','changeablenbg_purple','changeablenbg_blue'];
        if(notice_classes[count] === "changeablenbg_blue") {
        var count = 0
        }
    //#box is the note div itself
    $('#box').toggleClass(notice_classes[count]);
    count++;
    }); 
}); 

I also tried to loop the array using a for loop but to no avail.. Can someone who knows please guide me on doing this. 我也尝试过使用for循环来循环数组,但无济于事。.知道的人可以指导我执行此操作。 I'm sorry if i haven't explained it well enough, please ask anything you don't understand. 对不起,如果我还没有解释清楚,请问任何您不理解的问题。

You've kinda got some weird things going on in your code, but the biggest is that you're toggling which is like a light switch. 您的代码中发生了一些奇怪的事情,但是最大的是您正在切换,就像电灯开关一样。 Press it, and it turns on and it stays on. 按下它,它就会打开并保持打开状态。 Press it again and it turns off. 再按一次,它将关闭。 Here you press and it turns on and switches to the next color, and then you press and it turns on the second getting in the way of the first. 在这里按,它会打开并切换到下一种颜色,然后按并在第二种颜色会打开,而第二种会妨碍第一种。

Then you've got the dual count problem. 然后,您会遇到双重计数问题。 You're stepping through each color for each NBG but only changing box. 您正在逐步浏览每个NBG的每种颜色,但只能更改框。 Resetting the local count but not the bigger one. 重置本地计数,但不重置较大的计数。

My best advice, step through each line of code as if the computer were running it, and think what does this specific line do. 我最好的建议是,像浏览计算机一样在代码的每一行中逐步执行,并思考这行代码的作用。 Does it do what I think it does? 它会按照我的想法做吗?

Is that helpful? 有帮助吗?

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

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