use*_*130 8 javascript php function
我试图从PHP调用一个JavaScript函数.根据所有的例子我一直在看下面应该工作,但事实并非如此.为什么不?
<?php
echo "function test";
echo '<script type="text/javascript"> run(); </script>';
?>
<html>
<script type="text/javascript">
function run(){
alert("hello world");
}
</script>
</html>
Run Code Online (Sandbox Code Playgroud)
Upv*_*ote 16
你的HTML无效.你错过了一些标签.
并且你需要在声明它之后调用它,就像这样
<html>
<head>
<title></title>
<script type="text/javascript">
function run(){
alert("hello world");
}
<?php
echo "run();";
?>
</script>
</head>
<body>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
在这种情况下,您可以在方法声明之前放置运行,但只要将方法调用包装在另一个脚本标记内,脚本标记就必须在方法声明之后.
试试自己http://jsfiddle.net/qdwXv/
该函数必须在使用前声明
它应该是
<html>
<script type="text/javascript">
function run(){
alert("hello world");
}
<?php
echo "function test";
echo run(); ;
?>
</script>
</html>
Run Code Online (Sandbox Code Playgroud)