如何使用JQuery和PHP?

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?

help.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.

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:

index.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.

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

doukui4836
doukui4836 试着看看我如何在使用Ajax的jQuery加载的内容中包含JavaScript
9 年多之前 回复
ds0409
ds0409 help.php从index.php文件调用到哪里?
9 年多之前 回复

5个回答



你的javascript可能在页面在浏览器中完成渲染之前执行

请看一下jquery ready函数的例子</ p >

http://api.jquery.com/ready/ </ p>
</ div>

展开原文

原文

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

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

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>
    <body>
        <?php if ($var == true) {?>
            <p>Hello</p>
        <?php } ?>
    </body>
</html>

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

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

<?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()?

douju5062
douju5062 对于要格式化为代码的行,缩进必须至少为四个空格(或更多)。
9 年多之前 回复
dtjzpg5313
dtjzpg5313 缩进必须恰到好处,至少在我的经验中。
9 年多之前 回复
drlq92444
drlq92444 为什么这看起来如此混乱......我是stackoverflow的新手。
9 年多之前 回复



如上所述,您需要将jQuery代码包装在$(document).ready()中。</ p>

您应该停止以现在的方式进行操作并使用模板系统,使用适当的MVC架构来为您的页面提供服务。 如果你继续这样做,那对你来说真是太棒了。 你应该永远不要让PHP输出javascript代码,这应该由你以功能的方式在单独的文件中编写,然后包含在你的HTML文件中。</ p>
</ div>

展开原文

原文

As mentioned, you need to wrap your jQuery code inside $(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.

There is another good way like below:

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

echo $alert;
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐