当我输入这样的代码:
<p>Hello <? echo $name; ?>,How are you?</p>
它打印:
<p>Hello <!--? echo $name; ?-->,How are you?</p>
作为评论.我把它放在一个名为base.js的文件中,代码如下:
function showName() { document.getElementById("name").innerHTML = "<p>Hello <? echo $name; ?>,How are you?</p>"; }
所以我像这样嵌入.js文件:
<script type="text/javascript" src="base.js"></script>
因此,在更改< p id =“name”>< / p>之后我明白了:
<p id="name">Hello <!--? echo $name; ?-->,How are you?</p>
那是因为它不再是PHP.
原文链接:https://www.f2er.com/php/138845.html改成
<script type="text/javascript" src="base.PHP"></script> and have a <?PHP header("content-type:text/javascript"); $name = "..."; ?> function showName() { document.getElementById("name").innerHTML = "<p>Hello <?PHP echo $name; ?>,How are you?</p>"; }
要么
改成
function showName(name) { document.getElementById("name").innerHTML = "<p>Hello "+name+",How are you?</p>"; }
<script> // using json_encode to make the string safe for script injection. // Still needs quotes for a single string showName("<?PHP echo json_encode($name); ?>"); </script>