php变量赋值给js

时间:2022-12-09 07:55:03
$(document).ready(function(){
    
    
    <?php $f="'name'"?>
    var t=<?php echo $f?>;
    alert(t)
    
    })

 

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

 

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