2012-09-14 25 views
1

我试图使用JavaScript来填充下拉列表和我得到在IE8以下错误:是否“...超出了范围”。 KSLint中的错误导致'[1]为空或不是对象'错误?

消息:“1”为空或不是对象 行:59 字符:3 代码: 0

当我推代码通过调试器(在这种情况下,JSBin)时,可以看到的代码如下错误:

线59:变量$ selected_view = $( '#ai1ec-视图 - ' + matches [1]); ---'匹配'超出了范围。

JSBin中的这个错误是否与IE8中的错误相关?当代码在Chrome,FF或IE9上运行时,错误不会填充任何内容。

以下是有问题的代码片段。

// Make current view actively selected in view dropdown button. 
    var classes = $('body').attr('class').split(' '); 
    for (i in classes) { 
     // Extract current view from the body class. 
     var matches = /ai1ec-action-([\w]+)/.exec(classes[i]); 
     if (matches != null) break; 
    } 
    // Get the dropdown menu link of the active view. 
    var $selected_view = $('#ai1ec-view-' + matches[1]); 
    // Replace contents of dropdown button with menu link, plus the caret. 
    $('#ai1ec-current-view') 
     .contents() 
      .remove() 
      .end() 
     .prepend($selected_view.contents().clone()) 
     .append('<span class="caret"></span>'); 
    // Deactivate all dropdown menu items. 
    $('#ai1ec-view-dropdown .dropdown-menu li').removeClass('active'); 
    // Activate only currently selected dropdown menu item. 
    $selected_view.parent().addClass('active'); 
+0

JSBin警告看起来似乎是假的,Javascript没有块范围,所以'matches'变量实际上被作用于整个函数,因此在范围内。 – lanzz

回答

0

号的外的范围棉短绒提示/错误/消息可能是因为变量是在体的体块宣布为在回路;这应该工作。

然而,matches似乎是null - 导致异常。只要matches不为空,你确实会中断循环,但这并不能保证真正匹配的东西 - 循环可能会结束,并且matches仍然为空。

顺便说一句:你不应该使用for-in-loop来枚举split返回的数组的属性,你希望用正常的for-loop迭代这些项。

相关问题