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

Jquery 对象不支持IE中的属性或方法addClass,jquery,Jquery,我正在试着把这件事记在DIV里。 键向上向下运行。但我得到一个错误,对象不支持属性或方法addClass。 我的代码如下: var $activeslide = $('.display_box.active'); var targetslide = null; if (e.keyCode == 40) { if ($activeslide.next('.display_box').length) { targetslide = $activeslide.next('.d

我正在试着把这件事记在DIV里。 键向上向下运行。但我得到一个错误,对象不支持属性或方法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');

如果
keyCode
不是40、38或13,则从不将
targetslide
设置为元素。您需要检查以下内容:

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

您试图调试您的问题是什么?您是否尝试过在
addClass
jQuery方法之前记录
targetslide
的值?我想在最后在targetslide中添加Class。我理解其意图。我试图收集更多关于您自己调试问题的方法的信息,作为自己找到答案的方法。谢谢Barmar!它运行良好。我是新来的。我还有一些和上面一样的问题。我很快就会发帖的。感谢您的帮助。我希望您计划自己使用Javascript调试器来解决这个问题。你不应该依赖于它,所以作为思考和调试的替代品,它应该是所有其他方法都失败后的最后手段。是的。我试着调试它。我被困在这个问题,我不知道它需要检查。感谢您的帮助。因此,您在该行上放置了一个断点,并检查变量的值。这不是正常的调试过程吗?如果你不知道如何设置断点,