SyntaxError:意外的标记ILLEGAL

use*_*007 2 javascript php jquery google-chrome

我在chrome上遇到"SyntaxError:Unexpected token ILLEGAL"错误.

<?
$data = "this is description
         new line";
?>

$(".gantt").gantt({
  desc: "<? echo $data; ?>"
});
Run Code Online (Sandbox Code Playgroud)

错误在"this is description"和"new line"之间.为什么我不能进入那里?有没有办法解决这个问题?

Roc*_*mat 5

你不能在JavaScript字符串中有(未转义)新行.

您正在输出:

$(".gantt").gantt({
  desc: "this is description
         new line"
});
Run Code Online (Sandbox Code Playgroud)

之后的新行description无效.

你需要json_encode你的价值(是的,也json_encode适用于普通字符串).

$(".gantt").gantt({
  desc: <? echo json_encode($data); ?>
});
Run Code Online (Sandbox Code Playgroud)

请注意,我删除了引号. json_encode将为您添加报价.