PHP 在 Javascript 破坏脚本中生成的内容 [英] PHP generated content inside Javascript breaking script

查看:31
本文介绍了PHP 在 Javascript 破坏脚本中生成的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网站,其中包含一些带有淡入淡出效果的 Javascript 旋转内容.该脚本工作正常,直到我在其中添加 PHP while 循环,然后它失败了.我正在使用 PHP 从 wordpress 帖子中提取内容.

I have a website with some Javascript rotating content with a fade. The script works fine until I add a PHP while loop inside it, then it fails. I'm using the PHP to pull content from wordpress posts.

这里是隔离代码(http://jsfiddle.net/dzz8M/2/).如果您删除 PHP,您会注意到脚本可以正常工作,您可以在此处看到 (http://www.smartcallclaims.com/indexx.php/) 表明 php 输出正常.

here is the isolated code (http://jsfiddle.net/dzz8M/2/). You'll notice the script works as desired if you remove the PHP and you can see here (http://www.smartcallclaims.com/indexx.php/) that the php is outputting fine.

当您检查该链接的来源并从 JSfiddle 中查找 JavaScript 时,您会看到 PHP 在脚本中生成了它的内容.(我知道它添加了一个

标签,这不是问题)

When you check the source of that link and look for the JavaScript from the JSfiddle you'll see the PHP has generated it's content inside the script. (I know it adds a <p> tag, this is not the issue)

有人能告诉我为什么 PHP 会破坏 javascript 吗?

Can some please tell me why the PHP breaks the javascript?

推荐答案

你的问题是引号中的一个未转义的换行符:

Your problem is the one unescaped linebreak in the quotes:

"\&nbsp;&nbsp;&nbsp;&nbsp;<p>this is a good web test</p>
<div id='quoteauthor'>Ben Harrison</div>",

这不是有效的 JavaScript.你必须以某种方式逃避它,例如使用 str_replace() 在创建这些字符串的 PHP 函数中.

This is not valid JavaScript. You have to escape it somehow, for example like by using str_replace() in the PHP functions, that create those strings.

str_replace("\n","&nbsp;",$string)

这篇关于PHP 在 Javascript 破坏脚本中生成的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆