php - 使用jquery动态加载php

标签 php javascript jquery html

我正在尝试动态加载 PHP 文档,该文档在同一脚本上使用一些 jQuery 事件,因此加载网页后 jQuery 效果不起作用。

例如:

$(document).ready(
  function(){
     main();
     function main()
     {
       $('#game1').hover(
         function(){ $('#info1 p').slideDown('fast'); },
         function(){ $('#info1 p').slideUp('fast'); }
       );   
       $("#izquierda a").click(function()
       {
          $('#izquierda ul').hide();
          $('#content').slideDown();
          $('#menu').slideDown();
          $('#contentMin').hide();
          $("#gameContent").load("content.php", main()); // i try loading main() again so the effects would have some effect? doesn't work though
       });
    }               
});

在 content.php 中,我从数据库加载数据并将其放置在名为 #game1 的 div 中,该 div 使用悬停效果,但在加载 content.php 后它不起作用。如果没有从 content.php 加载它,相同的脚本也可以工作。 那么我怎样才能让它发挥作用呢? 抱歉,我是新手,谢谢!

最佳答案

由于服务器需要渲染代码,所以这里需要使用AJAX。查找 jQuery ajax 函数:http://api.jquery.com/jQuery.ajax/ .

$("#gameContent").ajax({
    url: "content.php",
    success: function(){ main(); }
});

关于php - 使用jquery动态加载php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8282546/

相关文章:

php - Codeigniter如何在 Controller 中使用jquery

php - 查找版本号是否在指定范围内

php - 在不失真的情况下调整图像大小,保持纵横比,然后使用 WideImage 裁剪多余部分

php - WooCommerce:如果购物车中已存在产品,则增加购物车商品数量

javascript - Node JS RegEx 非捕获组未按预期工作

javascript - 随机播放 jQuery 对象的一部分但返回整个对象

javascript - 如何将使用 php 上传的 pdf 文件和图像文件合并到 MySQL 中?

javascript - Electron 应用程序渲染图像不正确

javascript - 跨多个选择框拆分 JSON 数组

javascript - 为什么这个 JSON 字符串解析失败?