<!DOCTYPE html>
<html>
<body>
<input type="text" onkeypress="handleEvent(event)" name="FirstName" style="position: absolute; left: 50px;">
<script>
function handleEvent(e){
if(e.keyCode === 13){
<?php
echo "hello";
?>
}
}
</script>
</body>
</html>
如果我在函数中删除PHP代码并放置alert("something"),它可以正常工作。但是当我在PHP函数中输入echo时,没有显示"hello"。请帮助我,挣扎了2天。
PHP在运行时执行,您的代码创建如下内容:
function handleEvent(e){
if(e.keyCode === 13){
hello
}
}
echo是一个PHP函数,它简单地将"中的所有内容打印到标记中。那么就用php打印你手写的内容:
function handleEvent(e){
if(e.keyCode === 13){
<?php
echo "alert('"hello'");";
?>
}
}
echo "alert('"hello'");";
告诉php打印alert("hello");
。'"
确保echo打印"
字符本身(因为echo需要打印的字符串也写在"
中,所以您需要转义额外的字符串,或者写echo 'alert("hello");';
)