简体   繁体   English

jQuery添加和删除类

[英]jQuery adding and removing classes

I'm trying to create a very simple piece of code that allows you to move a red and blue ball around the screen - you click on the one you want to move then use the arrow keys. 我正在尝试创建一个非常简单的代码片段,允许您在屏幕上移动一个红色和蓝色的球 - 您单击要移动的那个,然后使用箭头键。 The part I'm struggling with is changing which ball is focused on, ie moved when you press the arrow keys. 我正在努力的部分是改变哪个球聚焦,即按箭头键时移动。

The plan is to add a .active class to ball you click on, while also removing the active class from the other. 计划是在你点击的同时添加一个.active类,同时从另一个中删除活动类。

I've got two simple divs, with the blue div initially set with the .active class: 我有两个简单的div,蓝色div最初设置为.active类:

<div id="blue" class="active"></div>
<div id="red"></div>

Here's the CSS: 这是CSS:

div {
    height: 100px;
    width: 100px;
    border-radius: 50%;
    border: 5px solid gray;
    margin: 20px;
    position: relative;

}

#blue {
    background-color: blue;


}    

#red {
    background-color: red;

}

Here's my javascript: 这是我的javascript:

$(document).ready(function() {

   $("body").keydown(function(key) {
        switch(parseInt(key.which,10)) {
            // Left arrow key pressed
            case 37:
                $('.active').animate({left: "-=10px"}, 1);
                break;
            // Up Arrow Pressed
            case 38:
                case 37:
                $('.active').animate({top: "-=10px"}, 1);
                break;
            // Right Arrow Pressed
            case 39:
                case 37:
                $('.active').animate({left: "+=10px"}, 1);
                break;
            // Down Array Pressed
            case 40:
                case 37:
                $('.active').animate({top: "+=10px"}, 1);
                break;
        }
    });
});

$(document).on("click", "#red", function () {
    $("#red").addClass(".active");
    $("#blue").removeClass(".active");
});

$(document).on("click", "#blue", function () {
    $("#blue").addClass(".active");
    $("#red").removeClass(".active");
});

I can move the blue ball around but clicking the red has no effect, the arrow keys continue to move the blue div. 我可以移动蓝色的球,但点击红色没有效果,箭头键继续移动蓝色div。

Here it is on jsfiddle - http://jsfiddle.net/7NF3L/ 这是jsfiddle - http://jsfiddle.net/7NF3L/

Any suggestions as to better ways to achieve the same thing would also be welcome. 任何关于更好地实现同样事情的建议也会受到欢迎。

The . . is used in front of the class name in a selector of a query and a rule to mark it as class. 查询选择器规则的类名前面用于将其标记为类。

The class name itself does not include the . 类名本身不包括.

So it has to be: 所以它必须是:

$("#red").addClass("active");
$("#blue").removeClass("active");

Use this 用这个

http://jsfiddle.net/7NF3L/1/ http://jsfiddle.net/7NF3L/1/

$("#red").click(function () {
    $(this).addClass("active");
    $("#blue").removeClass("active");
});

$("#blue").click(function () {
    $(this).addClass("active");
    $("#red").removeClass("active");
});

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

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