开发者

Move a div using keypresses and jquery?

Alright, so I'm trying to make a simple script that moves down a div when I press 's' but it's not firing at all. I got Jquery installed, and开发者_开发问答 the error-console in FF isn't saying anything. Can someone tell me what I'm doing wrong?

http://prime.programming-designs.com/designs/map/


You forgot to put // before your HTML open comment tag <!--

You shouldn't use HTML comment tags for scripts anyway, the best method for inline styles/scripts is to use the CDATA element as follows:

/* <![CDATA[ */
//your code in here
/* ]]> */

This does a number of things:

  • allows XML entities to not need to be escaped
  • use of multi-line comments prevents any newline issues that may result in your script being parsed (CMS's will often adjust white-space)
  • creates valid XML when used in XHTML


I suggest you use the jquery.hotkeys extension to bind to the key events: https://github.com/jeresig/jquery.hotkeys


Here's your code:

$('#map').keydown(function(event) {
    switch (event.keycode) {
        case 83: // left arrow key
            markX = markX + markSpeed;
            mark.style.top  = markX + 'px';
            break;
    }
});

The issues are that keycode needs to be camelCase keyCode and that the case should be 37 for the left arrow or 40 for the down arrow.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜