尝试这个:
<?php $htmlString= 'testing'; ?><html> <body> <script type="text/javascript"> // notice the quotes around the ?php tag var htmlString="<?php echo $htmlString; ?>"; alert(htmlString); </script> </body></html>
当您遇到此类问题时,一个好主意是检查浏览器是否存在Javascript错误。不同的浏览器有不同的显示方式,但是需要一个javascript控制台或类似的东西。另外,检查浏览器查看的页面源。
有时,初学者对字符串中的引号感到困惑:在PHP部分中,您分配
'testing'给
$htmlString。这将字符串值放入该变量内,但该值中没有引号:它们仅用于解释器,因此他知道:
哦,现在是字符串文字 。



