对象不支持IE中的属性或方法addClass

时间:2014-08-19 02:24:59

标签: jquery

我正试图在DIV中关闭事件。 key up down工作。但是我得到了对象不支持属性或方法addClass的错误。 我的代码如下:

var $activeslide = $('.display_box.active');
var targetslide = null;

if (e.keyCode == 40) {
    if ($activeslide.next('.display_box').length) {
        targetslide = $activeslide.next('.display_box');
    } else {
        $('.display_box').first().addClass('active');
        targetslide = $('.display_box:first');
    }
}

if (e.keyCode == 38) {
    if ($activeslide.prev('.display_box').length) {
        targetslide = $activeslide.prev('.display_box');
    } else {
        targetslide = $('.display_box:last');
    }
    e.preventDefault();
}
if (e.keyCode == 13) {
    alert($('.active.display_box').text());
}
targetslide.addClass('active');
$activeslide.removeClass('active');

1 个答案:

答案 0 :(得分:1)

如果keyCode不是40,38或13,则永远不要将targetslide设置为元素。你需要检查一下:

if (targetslide) {
    targetslide.addClass('active');
}