考虑这个简单的例子;
<?php $text = test; ?>
<script type="text/javascript" defer="defer">
var test;
test = "<?php echo $text; ?>"
$(document).ready(function(){
alert(test);
});
</script>
Run Code Online (Sandbox Code Playgroud)
这很好用.使用php var中的文本创建警报.但是,如果我放置;
<?php $text = test; ?>
Run Code Online (Sandbox Code Playgroud)
在脚本下面 - 它不起作用.我尝试过延迟功能.我究竟做错了什么?
干杯
如果你放置
<?php $text = "test"; ?>
Run Code Online (Sandbox Code Playgroud)
在JS代码下面,变量$ text尚未定义,因此您无法在脚本中提前回显(编辑).