php变量赋值给js

时间:2021-01-26 07:57:34

$(document).ready(function(){              <?php $f="'name'"?>    vart=<?php echo $f?>;    alert(t)         })

<script language="javascript">vart=<?php echo '"sd"'?>;alert(t)</script>

关键是sd两旁既要加单引号还要加上引号否则就会显示
“sd is not defined
[在此错误处中断]     
var t=sd;”
sd不加引号即为一个变量,之前没定义过该变量,就把他赋值给别的变量,当然会报错

来源:http://www.cnblogs.com/breakdown/archive/2012/04/23/2466469.html