Php Ajax如何获取函数参数example.html(html)
我从网站上获取代码,类似于文件名:index.php:Php Ajax如何获取函数参数example.html(html),php,jquery,ajax,Php,Jquery,Ajax,我从网站上获取代码,类似于文件名:index.php: <html> <body> <div class="menu" title="example">Click here to change the content from a html file</div> <div id="content"></div> <script> $(".menu").click(function() { var p
<html>
<body>
<div class="menu" title="example">Click here to change the content from a html file</div>
<div id="content"></div>
<script>
$(".menu").click(function() {
var page = $(this).attr("title");
$.post("content.php", {"page": page}, function(html){ $("#content").html(html);});
});
</script>
</body>
</html>
在content.php中:
<?php
ob_start();
include 'index.php';
ob_end_clean();
$page = $_POST['page'];
$file = $page."html";
include($file);
?>
它适用于简单的$page.html文件。然而,如果我在$page.html中放入一些复杂的JavaScript,它将无法工作。我想知道为什么,如何以及是什么。htmlhtml?我找不到任何关于以html形式传递参数的参考。这里的html是什么 它只是一个变量,包含对ajax请求的响应。$selector.htmlhtml是jQuery的 选择与选择器匹配的项目 将其内部html设置为名为html的变量 在您的示例中,名为html的变量是传递回$.post调用的success函数的参数。因此,$.post与服务器对话,获取页面,然后将内容加载到您的div中,id=content。因此,对于更复杂的页面,可能您没有正确设置返回的格式。在该函数中执行alerthtml,以一种非常简单的方式调试html设置。在firebug或Chrome的开发者控制台中设置一个断点,以便深入挖掘
function(html){ $("#content").html(html);}
部分代码将调用content.php返回的内容分配到ID设置为content的现有元素中。您完全可以将javascript函数加载到DOM中,或者在加载到content div时执行它们。您会遇到什么样的错误
我建议在您的回拨中发出警报,如:
function(html){ **alert(html);** $("#content").html(html);}
functionhtml中的html是从服务器接收的输出的任意名称。所以,$content.htmlhtml;正在将该信息插入到$'content'中。响应是否正确?如果您选择console.loghtml或alerthtml,它是您要查找的内容吗
不确定这是否有什么不同,但是如果您没有得到响应,您能用echo$html替换PHP中的include$html吗?谢谢您的回答!我现在明白部分: 函数HTML{ $content.htmlhtml;} 参数html不正确 违约这与: functionnew_content{$content.htmlnew_content;} 对于复杂的$page..html,我 只需将代码更改为: $.getpage+'.html',functionnew_page{$'content'.htmlnew_page;} 它很好用。然而,我仍然感到困惑,为什么我不能加载复杂的Javascript文件 通过使用: $.postcontent.php,{page:page},functionhtml{$content.htmlhtml;} 只是为了解释一下 如果是这样写的,也许你能更好地理解 .html“此处有一些返回的html”
括号内的html表示返回的html如果我将$page.html代码复制到中,则效果良好。请尝试使用Firebug或类似的调试器。我不确定人们是否能帮上大忙,因为看不到代码/html会破坏它。是的,我在中替换了$page.html文件,它工作得很好。alerthtml只显示整个文件的代码。什么是复杂的Javascript?检查这个,
$.post("content.php", {"page": page}, function(html) {
$("#content").html(html);
});