0

好久没上这个了,如有明显错误请见谅

    switch (event.keyCode) {
case 38:
    $("div.sseholdh").removeClass(function(){
    alert($(this).next().attr("title")); // doesn't alert
    $(this).next().addClass('sseholdh');
    return 'sseholdh';
    }); // does remove the class , but doesn't add it to the next sibling
break;
...
default:
    $("div.ssehold:eq(0)").addClass("sseholdh"); // does work
} // switch keycode

html 看起来像这样:

<div class="sshold">
  <div title="entry 1" class="ssehold"> entry 1 </div>
  <div title="entry 2" class="ssehold"> entry 2 </div>
...
</div>
4

2 回答 2

0

为什么不将您的 javascript 更改为以下内容 - 无需在removeClass()函数中添加类:

switch (event.keyCode) {
    case 38:
        $("div.sseholdh").removeClass('sseholdh').next().addClass('sseholdh');
        break; //...
    default:
        $("div.ssehold:eq(0)").addClass("sseholdh"); // does work
} // switch keycode

编辑:用于测试的基本 jsFiddle:http: //jsfiddle.net/greglockwood/wy9ZX/

编辑 2:这是一个更完整的版本,似乎可以使用实际站点上的标记来完成您尝试做的事情:

        switch (event.keyCode) {
        case 38:
            // up arrow
            var highlight = $("div.sseholdh");
            // if nothing highlighted already, highlight the bottom entry
            if (!highlight.length) {
                $('div.ssehold:last').addClass('sseholdh');
            } else {
                // otherwise, change to the previous entry
                highlight.removeClass('sseholdh').prevAll('.ssehold:first').addClass('sseholdh');
            }
            break;
        case 40:
            // down arrow
            var highlight = $("div.sseholdh");
            // if nothing highlighted, select the first entry
            if (!highlight.length) {
                $('div.ssehold:first').addClass('sseholdh');
            } else {
                // otherwise, move the highlight down
                highlight.removeClass('sseholdh').nextAll('.ssehold:first').addClass('sseholdh');
            }
            break;
        default:
            // nothing here, I'm assuming you only want to move the highlight with the arrow keys
        } // switch keycode

jsFiddle 在这里更新:http: //jsfiddle.net/greglockwood/wy9ZX/2/

于 2011-11-08T08:00:47.623 回答
0

如果我是你,我会首先给你的 div 一个唯一的 ID。也许这只是个人喜好,但我觉得它更容易工作。

接下来我会扔掉你所有的代码(从开关内)并从这个开始:

switch (event.keycode){ case 38: alert('Hello World.'); }

检查这是否有效。也许它只是你的开关不起作用。我查了一下,keyode 38 的“L”是否正确?

(这可能不是我知道的最佳答案,但我还不能发表评论,所以这是目前唯一的方法)

于 2011-11-08T07:54:48.430 回答