简体   繁体   中英

Javascript (jQuery) - Codecademy course

These are for two separate codecademy exercises. They both pass me but I'm not getting the result I'm supposed to.

3.2 - Mouse Events - Question

Write a hover handler and attach it to all divs. In the first function add the class "hover" to the current object we are hovering over, and in the second remove the class "hover". We have already learned how to do this by passing the event object, but this time let's try another way by using $(this).addClass() . Though, you can try events if you would like!

When you are done the green boxes should pop out and turn blue as the user hovers over them.

3.2 - Mouse Events - Answer

$(document).ready(function(){
    $('div').hover(function() {
        (this).addClass('hover');
    },
    function() {
        (this).removeClass('hover');
    });
});

3.3 Keyboard Events - Question

keypress is formatted exactly like the click handler.

Write a keypress handler that appends a div with class "box" to the div with id = "boxDiv". Attach the keypress handler to the body of the document.

3.3 Keyboard Events - Answer

$(document).ready(function(){
    $("body").keypress(function(event){
        $('#boxDiv').append($("<div/>").addClass('box'));
    });
 });

If you'd like further clarification here's a direct link to the course. http://www.codecademy.com/courses/jquery-events/2#!/exercises/1

Thanks in advance!

Regards,

Matt

Regarding mouse events, there's a minor syntax error:

(this).addClass('hover'); and (this).removeClass('hover');

are missing leading dollar signs. They should be:

$(this).addClass('hover'); and $(this).removeClass('hover');

As far as the keyboard test, it should work. When you run it, try clicking in the result area before pressing a key.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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