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