php - 语法错误 : Unexpected token ILLEGAL

标签 php javascript jquery google-chrome

我在 chrome 上遇到“SyntaxError: Unexpected token ILLEGAL”错误。

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

$(".gantt").gantt({
  desc: "<? echo $data; ?>"
});

错误在“this is description”和“new line”之间。为什么我不能在那里使用输入?有没有办法避免这种情况?

最佳答案

JavaScript 字符串中不能有(未转义的)新行。

你正在输出:

$(".gantt").gantt({
  desc: "this is description
         new line"
});

description 后的新行无效。

您需要json_encode您的值(是的,json_encode 也适用于纯字符串)。

$(".gantt").gantt({
  desc: <? echo json_encode($data); ?>
});

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

关于php - 语法错误 : Unexpected token ILLEGAL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13461728/

相关文章:

php - html php mysql 安全

javascript - Firefox 扩展中 loadSubScript 和 loadFrameScript 之间的区别

javascript - jQuery Mobile 弹出窗口未在 .popup ('open' 上打开)

javascript - 仅在提交表单时将按钮添加到导航菜单?

javascript - 从 2 个按 id 分组的 JSON 对象创建新的 javascript 对象

jquery - 如何使用 jQuery 更改 <span> 中的文本?

jquery - 使用 jQuery 验证,有没有一种方法可以在不引用创建的对象的情况下重置表单?

php - HTML 表单/PHP : select with option value = array

php - 为什么 "--comment"不工作而 "-- comment"工作?

javascript - 需要帮助对 Google Charts 时间线中的神秘消息进行故障排除 ['undefined' 不是对象(评估 'this.Vn[0].v' )]