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

    评论

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?