Uncaught TypeError:无法读取未定义的属性'indexOf'
问题描述:
我想让文档之外的显示功能准备就绪。如果时间等于零,它必须向我证明正确与错误。但我得到一个错误**遗漏的类型错误:无法读取的未定义的属性“的indexOf” **Uncaught TypeError:无法读取未定义的属性'indexOf'
$(document).ready(function() {
droppableId = $(this)[0].id;
draggableId = ui.draggable[0].id;
var show = function(ui, droppableId, draggableId) {
if (wordMap[droppableId].indexOf(draggableId) != -1) {
ui.draggable.removeClass("ui-state-right").addClass("ui-state-right");
} else {
ui.draggable.removeClass("ui-state-right").addClass("ui-state-wrong");
}
}
});
var seconds = 60;
function secondPassed() {
var minutes = Math.round((seconds - 30)/60),
remainingSeconds = seconds % 60;
if (remainingSeconds < 10) {
remainingSeconds = "0" + remainingSeconds;
}
document.getElementById('countdown').innerHTML = minutes + ":" + remainingSeconds;
if (seconds == 00) {
clearInterval(countdownTimer);
document.getElementById('countdown').innerHTML = "0:00";
if (corrects >= 4) {
$("#successAlert").show();
$("#startAgainGameBtn").show();
} else {
$("#resultBtn").prop('disabled', true);
show();
$("#getSol").show();
}
} else {
seconds--;
}
}
var countdownTimer = setInterval('secondPassed()', 1000);
答
试试这个代码,在代码中你没有传递变量到函数因此未定义错误。这样变量就可以直接在函数中使用了。然而,这是未经测试,因为没有任何HTML测试。
$(document).ready(function() {
var droppableId = $(this)[0].id;
var draggableId = ui.draggable[0].id;
window.show = function() {
if (wordMap[droppableId].indexOf(draggableId) != -1) {
ui.draggable.removeClass("ui-state-right").addClass("ui-state-right");
} else {
ui.draggable.removeClass("ui-state-right").addClass("ui-state-wrong");
}
}
});
var seconds = 60;
function secondPassed() {
var minutes = Math.round((seconds - 30)/60),
remainingSeconds = seconds % 60;
if (remainingSeconds < 10) {
remainingSeconds = "0" + remainingSeconds;
}
document.getElementById('countdown').innerHTML = minutes + ":" + remainingSeconds;
if (seconds == 00) {
clearInterval(countdownTimer);
document.getElementById('countdown').innerHTML = "0:00";
if (corrects >= 4) {
$("#successAlert").show();
$("#startAgainGameBtn").show();
} else {
$("#resultBtn").prop('disabled', true);
window.show();
$("#getSol").show();
}
} else {
seconds--;
}
}
var countdownTimer = setInterval('secondPassed()', 1000);
+0
其实这是一个巨大的编码它,我创建可拖动的元素动态,如果你想看到我会发布在这里 – babuharry
你可以显示你的HTML吗? –
尝试使'show()'窗口变量,所以它会'window.show = function(){}'然后你会使用window.show引用它 – Deckerz
错误的原因是'wordMap [droppableId]'做不提及任何东西。你期待它成为什么? –