考虑这个简单的例子;
<?php $text = test; ?>
<script type="text/javascript" defer="defer">
var test;
test = "<?php echo $text; ?>"
$(document).ready(function(){
alert(test);
});
</script>
这很好用。使用 php var 中的文本创建警报。但是,如果我放置;
<?php $text = test; ?>
在脚本下方 - 它不起作用。我试过延迟功能。我究竟做错了什么?
干杯
最佳答案
如果你放置
<?php $text = "test"; ?>
在 JS 代码下面,变量 $text 还没有定义,所以你不能在脚本中 earlier (edit) 回显它。
关于Javascript 中的 PHP 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6214321/