我想知道这段代码有什么问题,如果我添加 anotherFunction
部分,它会抛出一个语法错误:意外标记“anotherFuction。有人可以解释一下原因吗, JavaScript 让我对调用函数的所有不同方式感到非常困惑。
var ajax = {
parseJSONP : function(result) {
console.log(result)
//iterate each returned item
$.each(result.items, function(i, row) {
$('#listview_test').append('<li><a href="#headline"><img src="' + row.volumeInfo.imageLinks.thumbnail + '" class="ui-li-has-thumb"/><h3>' + row.volumeInfo.title + '</h3></a></li>');
});
//end iteration of data returned from server and append to the list
$('#listview_test').listview('refresh');
// refresh the list-view so new elements are added to the DOM
}
//error is here, I just wanted to add another function here to do
something
anotherFuction : function(){
}
}
}
最佳答案
您忘记了对象字面量中第一个成员的结束 }
之后的 ,
。
您需要将每个成员分开,如下所示:
var ajax = {
parseJSONP : function(result) {
$.each(result.items, function(i, row) {
$('#listview_test').append('<li><a href="#headline"><img src="' + row.volumeInfo.imageLinks.thumbnail + '" class="ui-li-has-thumb"/><h3>' + row.volumeInfo.title + '</h3></a></li>');
});
$('#listview_test').listview('refresh');
},
// ^ Here
anotherFuction : function(){
// No more syntax errors!
}
}
关于javascript - jQuery 对象字面量函数声明错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16874697/