我正在尝试从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>

最佳答案

您的html无效。您缺少一些标签。

并且您需要在函数声明后调用它,就像这样

<html>
    <head>
       <title></title>

       <script type="text/javascript">
            function run(){
                alert("hello world");
            }

           <?php
               echo "run();";
           ?>
       </script>

    </head>
    <body>
    </body>
</html>

在这种情况下,您可以将运行放在方法声明之前,但是一旦将方法调用包装在另一个脚本标签中,脚本标签就必须在方法声明之后。

尝试一下http://jsfiddle.net/qdwXv/

关于php - 从php调用javascript函数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12812842/

10-12 12:50
查看更多