【问题标题】:Unexpected Identifier Error after Ajax callAjax 调用后出现意外的标识符错误
【发布时间】:2013-12-16 10:46:36
【问题描述】:

我有这个 HTML 代码:

 <div id="texttheory" class="centertext">'. $short .' </div>';
 <button id="thbutton" class="theory_button" onclick="javascript:changetheory('.$long.')">
     <img id="imagebutton" src="./images/arrows.png" width="15px" heigth="22px">
 </button>

我在一个由 javascript 函数调用的 php 脚本中生成:

function showTheory (kl) {
     var xmlhttp;
     if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
     }
     else {// code for IE6, IE5
          xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
     }
     xmlhttp.onreadystatechange=function() {
            if (xmlhttp.readyState==4 && xmlhttp.status==200) {
                 document.getElementById("theory").innerHTML=xmlhttp.responseText;
            }
     }
     xmlhttp.open("GET","gettheory.php?kl="+kl,true);
     xmlhttp.send();
 }

函数 changetheory 在 js 库中正确包含在 html 头标签中,它的工作原理如下:

function changetheory (content, old) {
     document.getElementById("texttheory").innerHTML= content;
     document.getElementById("thbutton").setAttribute ('onclick', "javascript:changetheory("+old+", "+content+")");
}

当我单击 thbutton 切换两个文本时,我得到错误: 未捕获的 SyntaxError:意外的标识符 在 html 文件的第 1 行。 任何人都可以看到问题是什么?

【问题讨论】:

    标签: javascript php html ajax


    【解决方案1】:

    您在 onclick 事件的内容字符串周围缺少引号。

    <button id="thbutton" class="theory_button" onclick="javascript:changetheory('.$long.')">
    

    应该是

    <button id="thbutton" class="theory_button" onclick="javascript:changetheory(\''.$long.'\')">
    

    【讨论】:

    • 非常感谢朋友。我现在感觉有点傻! :) 我会尽快接受答案!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-01-28
    • 2014-10-05
    • 1970-01-01
    • 2017-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多