jQuery 未捕获的语法错误:意外的令牌函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12254197/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Uncaught SyntaxError: Unexpected token function
提问by user1644644
I keep getting the "unexpected token function" error when trying to run the following code. The error appears at the if(highPriority(priority)) {
line. I appreciate any help, thanks.
尝试运行以下代码时,我不断收到“意外的令牌函数”错误。错误出现在该if(highPriority(priority)) {
行。我感谢任何帮助,谢谢。
$(function(){
$("input[name=task]").focus();
$("body").on("click","#add",function(){
var task = $("input[name=task]").val();
var priority = $(this).parent().children("input[name=priority]");
var doneSpan = '<span class="done">Done</span>';
if(highPriorityChecked(priority)) {
$("#todo").prepend("<p class='row high-priority'>" + task + doneSpan + "</p");
} else {$("#todo").append("<p class='row normal-priority'>" + task + doneSpan + "</p");
};
resetForm();
doneList();
});
}
function highPriorityChecked(priority){
return $("input[name=priority]").is(":checked");
};
function doneList(){
$("#todo").on("click",".done",function(){
var row = $(this).parent().detach();
$("#done").prepend(row).remove(doneSpan);
});
}
function buildRow(task, priority) {
var row = '<div class="row item ' + priority + '">' + task;
var doneSpan = '<span class="done">Done</span>';
return row + doneSpan + "</div>";
};
function resetForm() {
$("input[name=task]").val("").focus();
$("input[name=priority]").removeAttr("checked");
};
回答by Day
adenogave the right answer in his comment
You didn't close the
document.ready
function properly, it's just closed by a single curly bracket.
您没有
document.ready
正确关闭该功能,它只是由一个大括号关闭。
He suggested you might have notice if you had formatted your code properly. But we can do better than that.
他建议您可能已经注意到您是否正确格式化了代码。但我们可以做得更好。
In general, when you get a syntax error in JavaScript, try running the entirescript through jslint. This may help point you at the cause of the error.
通常,当您在 JavaScript 中遇到语法错误时,请尝试通过jslint运行整个脚本。这可能有助于指出错误的原因。