javascript 未捕获的语法错误:意外的标识符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8777407/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 04:32:24  来源:igfitidea点击:

Uncaught SyntaxError: Unexpected identifier

javascriptfunction

提问by Dvir Levy

hello im am getting JS error :

你好,我收到 JS 错误:

 Uncaught SyntaxError: Unexpected identifier

here

这里

<script type="text/javascript">
var cur_level = 1;
var ids_arr = new Array(<?php echo $max_level?>);
var im_here = new Array(<?php echo $max_level?>);
ids_arr[0] = 1;
im_here[0] = "|";
function displayData(id, level, used, title)
{
if(used){
    choice = document.getElementById('divv'+id).innerHTML;
    document.getElementById('test_div').innerHTML = choice;

} else {
    document.getElementById('test_div').innerHTML = ' No lerning paths to show.';
    updateLinksDiv(id, level, title);

  }
}

function updateLinksDiv(id, level, title)
{
var links_div_info = document.getElementById('links_parent_'+id);
var tmpHTML = '';
var here = '';

for(i=0;i<level;i++){
    here+= '->'+im_here[i];
    links_div_info = document.getElementById('links_parent_'+ids_arr[i]);
    tmpHTML += '<div id="divl_'+links_div_info.id+'">'+links_div_info.innerHTML+'</div>';
}
links_div_info = document.getElementById('links_parent_'+id);
tmpHTML += '<div id="divl_'+links_div_info.id+'">'+links_div_info.innerHTML+'</div>';

document.getElementById('links').innerHTML = tmpHTML;
ids_arr[i] = id;
im_here[i] = title;
}

</script>


<script type="text/javascript">
    window.onload=updateLinksDiv(1 , 0 , "|" ) ;
</script>

the functions are suppose to create an "expanding" that opens up with levels and everything was working fine untill i added the "title" and i started getting the error. the error points me to the last and i just cant find the error... i try to call displayData like this

这些函数应该创建一个“扩展”,它会随着级别打开并且一切正常,直到我添加了“标题”并且我开始收到错误消息。错误将我指向最后一个,我只是找不到错误...我尝试像这样调用 displayData

onclick="displayData('.$cat->id.','.$cat->level.',0,'.$cat->title.')"

any suggestions for what i'm not seeing.?

对于我没有看到的任何建议。?

thank you

谢谢

回答by Jukka K. Korpela

In your comment you say that displayData(26,1,0,???? ?)is generated. This explains the symptoms, as here the last parameter contains a space in addition to Hebrew letters, so the JavaScript intepreter sees it as two identifiers separated by a space, and the identifiers are probably undefined. Google Chrome gives the error message you describe, whereas Firefox and IE say, more enigmatically, “missing ) after argument list.”

在您的评论中,您说这displayData(26,1,0,???? ?)是生成的。这解释了症状,因为这里的最后一个参数除了希伯来字母之外还包含一个空格,因此 JavaScript 解释器将其视为由空格分隔的两个标识符,并且标识符可能未定义。谷歌浏览器给出了你描述的错误信息,而 Firefox 和 IE 更神秘地说,在参数列表之后“缺少 )”。

Apparently the generated code is supposed to have the last parameter in quotation marks, i.e. '???? ?'. You need to modify the generation to contain them.

显然,生成的代码应该在引号中包含最后一个参数,即 '???? ?'。您需要修改生成以包含它们。