运行代码段然后点击点击此处!,会出现错误提示:
{" message":" Uncaught TypeError:ll_ctq不是函数",
" filename":" https://stacksnippets.net/js"," lineno":15," colno": 93}
但我的代码中有一个函数ll_ctq()
。
var web = 'http://localhost/prjct/';
var ll_50_50 = 'll_50_50=';
var ll_ctq = 'll_ctq=';
var ll_dd = 'll_dd=';
var and = '&';
var play = 'play.php?';
function ll_ctq() {
var check_ll_ctq = document.getElementById("ll_ctq"),
classes = ['red_cross'];
if (check_ll_ctq.classList.contains("red_cross")) {
// alert("Yes");
} else {
// alert("No");
document.getElementById("ll_ctq").classList.add("red_cross");
document.getElementById("A1").setAttribute('class', 'timeout');
document.getElementById("B1").setAttribute('class', 'timeout');
document.getElementById("C1").setAttribute('class', 'timeout');
document.getElementById("D1").setAttribute('class', 'timeout');
var check_ll_ctq = document.getElementById("ll_ctq"),
classes = ['red_cross'];
var check_ll_50_50 = document.getElementById("ll_50_50"),
classes = ['red_cross'];
var check_ll_dd = document.getElementById("ll_dd"),
classes = ['red_cross'];
if (check_ll_ctq.classList.contains("red_cross")) {
var ll_ctq_status = "yes";
} else {
var ll_ctq_status = "no"
}
if (check_ll_50_50.classList.contains("red_cross")) {
var ll_50_50_status = "yes";
} else {
var ll_50_50_status = "no";
}
if (check_ll_dd.classList.contains("red_cross")) {
var ll_dd_status = "yes";
} else {
var ll_dd_status = "no";
}
var location = web + play + ll_50_50 + ll_50_50_status + and + ll_ctq + ll_ctq_status + and + ll_dd + ll_dd_status;
setTimeout(function() {
window.location = location;
}, 3000);
}
}

<div id="play_head">
<div>
</div>
<div>
<button id="ll_ctq" class="button_cstm_ll" style="margin-right:45px" onclick="ll_ctq()">Click here!</button>
</div>
<div>
</div>
</div>
<table id="test" class="table table-responsive table_cstm">
<tbody>
<tr>
<td colspan="2">
<div class="question1" style="justify-content: center; width: 100%;">1) Who was First Prime Minister of India?(15304)</div>
</td>
</tr>
<tr>
<td>
<div id="A1">A) M K Gandhi
</div>
</td>
<td>
<div id="B1">B) Jawaharlal Nehru
</div>
</td>
</tr>
<tr>
<td>
<div id="C1">C) Rajendra Prasad
</div>
</td>
<td>
<div id="D1">D) Chandra Shekhar Azad
</div>
</td>
</tr>
</tbody>
</table>
&#13;
运行代码段然后点击点击此处!,会出现错误提示:
{&#34; message&#34;:&#34; Uncaught TypeError:ll_ctq不是函数&#34;,
&#34; filename&#34;:&#34; https://stacksnippets.net/js&#34;,&#34; lineno&#34;:15,&#34; colno&#34;: 93}
但我的代码中有一个函数ll_ctq()
。
答案 0 :(得分:5)
您定义ll_ctq
两次。
首先使用挂起的函数声明(这就是为什么它首先出现在代码的后面)。
第二个用var ll_ctq = 'll_ctq=';
用字符串覆盖它。
所以当你来调用它时,它是一个字符串,而不是一个函数。
答案 1 :(得分:1)
您的代码确实创建了一个名为“ll_ctq”的函数,但是代码声明了一个名称完全相同的变量,因此该函数将丢失。
函数和变量共享相同的命名空间。
函数声明在var
声明之上悬挂,所以即使函数声明出现在它们之后,它实际上也是先解释的。