dongruolin5324 2011-04-14 20:05
浏览 39

如何使用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?

  • 写回答

5条回答 默认 最新

  • duanlv5084 2011-04-14 20:08
    关注

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

    评论

报告相同问题?

悬赏问题

  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况