2011-07-27 75 views
1

我有php和jQuery/Javascript之间的问题......它不会显示或无法工作。PHP/jQuery未捕获SyntaxError:意外的令牌非法

我的代码:

if($bottom_1_banner == "true"){ 
     //$data_AD .= '$(\'.ban_bottom\').html(\''.htmlentities($bottom_banner).'\').text();'; 
     $data_AD .= '$(\'.ban_bottom\').html("'.htmlspecialchars($bottom_banner).'").text();'; 
} 

错误日志:(铬/ Safari浏览器)

<script type="text/javascript"> 
    $(document).ready(function() { 
     $('.ban_bottom').html("&lt;!-- xxxxxxx --&gt; 
******index.php:11 Uncaught SyntaxError: Unexpected token ILLEGAL****** 
      &lt;script type=&quot;text/javascript&quot;&gt; 
      xxxxxxx_bid = &quot;xxxxxxxxxxx&quot;; 
      &lt;/script&gt; 
      &lt;script type=&quot;text/javascript&quot; src=&quot;http://xxx.xxxxxx.com/k.js&quot;&gt;&lt;/script&gt; 
      &lt;!-- xxxxxxxx --&gt;").text(); }); 
</script> 

OR

Error Log

编辑:转换后的图像为文本。根据您的代码

+0

删除htmlentities并可能使用addslashes ... –

+1

这很难阅读。你可以发布代码而不是代码的隐藏图片吗? – David

+0

您可能需要告诉htmlentities使用UTF8编码。 – Treffynnon

回答

0

上面,有一两件事是肯定的,则需要使用.=之前定义$data_AD变量或只使用=没有.

+0

nope ...我有4个脚本添加到一个变量=)但我测试一个脚本第一..其中3脚本后。 – user453089

+0

也许你也需要检查这个线程:http:// stackoverflow。com/questions/5733275/chrome-uncaught-syntax-error-unexpected-token-illegal – toopay

0

也许做

$data_AD .= '$(\'.ban_bottom\').html("'.$bottom_banner.'").text();'; 

... 

<?php echo str_replace('\n', '\\n', $data_AD); ?> 

删除换行符?

+0

nope still no work =/ – user453089

+0

@ user45:htmlspecialchars有什么用途? – marc

+0

非法,实际上有新的路线。它不能不同,因为它是一个字符串内的非法字符!遵循marc解决方案并检查结果是什么。 –

0

摆脱HTML中的新行。 Javascript不理解多行的字符串。您也可以替换成\ n与\ \ N:

这不起作用:

var a = "This is a 
string for me"; 

这工作

var a = "This is a \ 
string for me"; 

var a = "This is a string for me"; 
+0

nope只有问题 ... jQuery不能创建脚本..但我发现了类似的链接:http://stackoverflow.com/questions/610995/jquery-cant-append-script-element – user453089

0

这行代码作品这里完美无瑕。 我猜想它的.php文件的文件编码问题。检查坏字符。

1

我在JavaScript中遇到了同样的问题。如果你从MySQL中获取值,你可以试试这个:

$php_string = str_replace('<br />','<br />\\',nl2br($php_string)); 

先用一个html标签<br/>更换断行,然后将生成<br/>标签后添加\;所以JS行总是以\结尾,表示JavaScript代码中正确的新行。

+0

这对我来说非常合适。 –

相关问题