javascript 如何在javascript文件中插入php代码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22492160/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to insert php code in a javascript file
提问by Jaeeun Lee
I'm trying to use a php code snippet for jQuery Cycle Slideshow, but the console gives me this error:
我正在尝试为 jQuery Cycle Slideshow 使用 php 代码片段,但控制台给了我这个错误:
"Uncaught SyntaxError: Unexpected token < "
“未捕获的语法错误:意外的标记 <”
var startingSlide = <?php echo $_GET["thumb"] ?>;
$(slideshowContainer).cycle ({
startingSlide: startingSlide
});
How can I use the PHP code correctly?
如何正确使用 PHP 代码?
I'd appreciate you help.
我很感激你的帮助。
回答by adeneo
PHP doesn't parse files with a .js
ending, but you could have the PHP parser parse javascript files, but that's a really bad idea, instead insert the PHP in the PHP file where it belongs, and get the data with javascript
PHP 不解析带有.js
结尾的文件,但您可以让 PHP 解析器解析 javascript 文件,但这是一个非常糟糕的主意,而是将 PHP 插入到它所属的 PHP 文件中,并使用 javascript 获取数据
<div id="some_element" data-startingslide="<?php echo $_GET["thumb"] ?>"></div>
then do
然后做
var startingSlide = $('#some_element').data('startingslide')
$(slideshowContainer).cycle ({
startingSlide: startingSlide
});