【问题标题】:SyntaxError: missing } after property list javascriptSyntaxError: 在属性列表 javascript 之后缺少 }
【发布时间】:2015-07-31 10:09:23
【问题描述】:

我收到以下错误。

错误

语法错误:属性列表后缺少 }

内容:Al futaim, 贸易公司<br />Saih Shuaib 3 36 号楼 M ——

PHP 代码

$content=$servicecenter->getCompanyName()."<br />".$servicecenter->getAddress()."<br /><button type='button' value='Get Direction' class='button' onclick='closeInfoWindow(),calcRoute()' name='Get Direction'>Get Direction</button>"; 

脚本

var infowindow = new google.maps.InfoWindow({
  content:<?php echo $content; ?>;
});

【问题讨论】:

  • 您需要添加引号(双引号或单引号都可以)
  • 添加更多信息:您需要添加引号(双引号或单引号都可以),删除 php 结束标记后的分号并记住转义输出(即尝试添加斜杠或 htmlentities )
  • 添加引号后出现错误 SyntaxError: unterminated string literal content:

标签: javascript php google-maps


【解决方案1】:

使用json_encode并删除行尾的分号:

content: <?php echo json_encode($content); ?>  /* no ; here! */

【讨论】:

  • 这实际上是最好的解决方案......你不必关心它是如何输出的,它总是会做对;)
  • 直到一个删除线分号看起来很可怕:)
【解决方案2】:

缺少quotes 的内容,不需要; -

content: '<?php echo $content; ?>'

或

content: <?php echo json_encode($content); ?>

【讨论】:

  • 不应该是双引号",因为字符串中已经有单引号了吗?
  • 分号是一个偏好问题。有些人喜欢,有些人不喜欢......不能争辩使用......对不起,你的意思是在JavaScript中 - 是的,这会破坏。在 php 代码中它是可选的...
  • @Svengali 你不需要转义你输出到浏览器的任何可能有危险的东西吗? :)
  • 使用 json_encode 生成 JavaScript 字符串比手动添加引号更安全。
  • stdob--'s answer在这种情况下更好。
【解决方案3】:
var infowindow = new google.maps.InfoWindow({
    content:<?php echo $content; ?>
});

您不能在对象的声明中包含;。如果要分隔属性,请使用,。

此外,根据您想要在此处回显的内容,您可能需要在 php 脚本周围添加 "。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多