未捕获的语法错误:jQuery中出现意外的标记ILLEGAL

Uncaught SyntaxError: Unexpected token ILLEGAL in jQuery

本文关键字:意外 ILLEGAL jQuery 语法 错误      更新时间:2023-09-26

运行Splunk示例时,我在这个函数中遇到了一个错误。

var injectCode = function(code) {
    var sTag = document.createElement("script");
    sTag.type = "text/javascript";
    sTag.text = code;
    $(head).append(sTag);
    return sTag;
}

确切的错误在$(head).append(sTag);中。它被放置在一个Jade文件中,并在Node上运行。我在这里做错了什么?

EDIT-抱歉,head被定义为函数正上方的var head = $("head");

code来自这个函数

var getCode = function(id) {
    var code = "";
    $(id + " pre li").each(function(index, line) {
        var lineCode = "";
        $("span" ,line).each(function(index, span) {
            if ($(span).hasClass("com")) {
                lineCode += " ";
            }
            else {
                lineCode += $(span).text();
            }
        });
        lineCode += "''n";
        code += lineCode;
        });
      return code;
    }

head是一个标签,使用它:

$('head').append(sTag);

编辑:

我想说删除这个:

lineCode += "''n";

尝试用lineCode += "''n";交换

lineCode += "'n";

我假设您处理的是一个代码字符串(要向其中添加换行符),而不是字符串文字字符串(要将'n文字添加到其中)。