【发布时间】:2015-04-13 09:10:40
【问题描述】:
每当我尝试在数组中添加单引号时,Uncaught SyntaxError: Unexpected identifier error occurs at line number 5。
我无法在搜索中使用任何转义字符。我需要能够输入 St George's Station 并且应该调用 liveSearch 方法来进行搜索。
我知道单引号是 javascript 中的一个特殊字符。有什么办法可以绕过这个错误吗?
P.S 我还是 javascript 的新手,所以请放轻松:)。
var htmlStr = "<ul class='list-group scrollable-menu'>";
for(var i=0; i<arrOfSuggestText.length; i++){
htmlStr += "<li class='list-group-item '>";
if(arrOfSuggestText[i] != "null"){
htmlStr += '<a id="searchResult'+i
+'" href="javascript:liveSearch(\''+arrOfSuggestText[i]+'\')" > '+
arrOfSuggestText[i]+'</a>';
}
htmlStr += "</li>";
}
htmlStr += "</ul>";
document.getElementById('searchResultList').innerHTML = htmlStr;
}
【问题讨论】:
-
I remember this code :) 您应该使用 DOM 操作而不是 innerHTML 和内联事件处理程序,那么撇号、引号或其他方式不会有任何问题。
标签: javascript innerhtml