Javascript 如何使用 JQuery 和 PHP?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5669018/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 18:22:33  来源:igfitidea点击:

How to use JQuery and PHP?

phpjavascriptjquery

提问by Tom Green

I am creating a PHP App and am using JQuery but have run into problems. For example, below is the code for a page i am writing, the alert() function calls when the pages loads but the second javascript Jquery code does not, I cannot see why one line works but the second does not?

我正在创建一个 PHP 应用程序并使用 JQuery,但遇到了问题。例如,下面是我正在编写的页面的代码,alert() 函数在页面加载时调用,但第二个 javascript Jquery 代码没有,我看不出为什么一行有效但第二行不行?

help.php

帮助.php

<?php
    echo "<script>";
        echo "alert(\"Loaded\");";   
        echo "$(\"#newDiv\").draggable().resizable();";
    echo "</script>";
    echo "<div id=\"newDiv\">";
    echo "</div>";
?>

The page calls the alert and creates the div but does not execute the jquery statement.

页面调用alert并创建div但不执行jquery语句。

I think this may be due to have my page is structures and loaded. pages are loaded inside a div using Jquery and AJAX so the only page the user actually "loads" is an index.php page like so:

我认为这可能是由于我的页面是结构和加载的。页面使用 Jquery 和 AJAX 加载到 div 中,因此用户实际“加载”的唯一页面是 index.php 页面,如下所示:

index.php

索引.php

<html>
<head>
    <title>BluePrintr Web Application.</title>
    <link rel="stylesheet" type="text/css" href="public.css" />
    <script type="text/javascript" language="javascript" src="jquery.js"></script>
    <script type="text/javascript" language="javascript" src="myLibs.js"></script>
</head>
<body>
<?php
    echo "<div id=\"web_Page\">";
        //////////////////////////////////////////////////////
        echo "<div id=\"web_Header\">";
            require("public/templates/header.php");
        echo "</div>";
        //////////////////////////////////////////////////////
        echo "<div id=\"web_Menu\" class=\"horizontalcssmenu\">";
            require("public/templates/menu.php");
        echo "</div>";
        //////////////////////////////////////////////////////
        echo "<div id=\"web_Content\">";
        echo "</div>";
        //////////////////////////////////////////////////////
        echo "<div id=\"web_Footer\">";
            require("public/templates/footer.php");
        echo "</div>";
    echo "</div>";
?>

The menu loads and then any page selected from this menu is then loaded into the "#web_Content" div. So when the help.php page is selected from the menu, it injects the script into the div.

菜单加载,然后从此菜单中选择的任何页面加载到“#web_Content”div 中。因此,当从菜单中选择 help.php 页面时,它会将脚本注入到 div 中。

Can anyone see why the javascript code will not execute on help.php?

谁能看出为什么 javascript 代码不会在 help.php 上执行?

回答by Adam

You need to wrap the jquery to initialize only after the page loads

只需要在页面加载后包装jquery进行初始化

<?php
    echo "<script type='text/javascript'>";
        echo "alert(\"Loaded\");";  
        echo "$(function(){$(\"#newDiv\").draggable().resizable();});";
    echo "</script>";
    echo "<div id=\"newDiv\">";
    echo "</div>";
?>`

The $(function(){});will cause the code to wait until after the page has been completely loaded.. I'm assuming you are also loading jQuery UI as you are using .draggable()?

$(function(){});将导致代码等到页面完全加载后.. 我假设您还在使用时加载 jQuery UI .draggable()

回答by Nik

Do this:

做这个:

<script type='javascript'>
    $(document).ready (function() {
        alert('Hi');
        // Other initialization
    });
</script>

Also, just write all of that as HTML. Use PHP like this:

另外,只需将所有内容都写为 HTML。像这样使用 PHP:

<html>
    <body>
        <?php if ($var == true) {?>
            <p>Hello</p>
        <?php } ?>
    </body>
</html>

If $varis false, the page is blank. This makes things much more readable.

如果$var为 false,则页面为空白。这使事情更具可读性。

回答by bumperbox

your javascript may be executing before the page has finished rendering in the browser have a look at the jquery ready function for examples

您的 javascript 可能会在页面在浏览器中完成渲染之前执行,请查看 jquery ready 函数的示例

http://api.jquery.com/ready/

http://api.jquery.com/ready/

回答by sciritai

As mentioned, you need to wrap your jQuery code inside $(document).ready().

如前所述,您需要将 jQuery 代码包装在 $(document).ready() 中。

You should stop doing it the way you are now and use a templating system, with a proper MVC architecture for serving your pages. It's just gonna be hell for you if you keep doing it this way. You should never have PHP outputting javascript code, this should be written by you in seperate files in a functional way and then included in your HTML file.

您应该停止以现在的方式这样做,并使用模板系统,并使用适当的 MVC 架构来为您的页面提供服务。如果你继续这样做,那对你来说就是地狱。您不应该让 PHP 输出 javascript 代码,这应该由您以功能方式编写在单独的文件中,然后包含在您的 HTML 文件中。

回答by Rashad

There is another good way like below:

还有另一种好方法,如下所示:

$alert = <<<LABEL
<script>
    $(document).ready (function() {
        alert('Hi');
        // Other initialization
    });
</script>
LABEL;

echo $alert;