如何使用fancybox加载php页面?
我搜索了很多,但我找不到答案。如何使用fancybox加载php页面?
在我contact.php我有这样的代码:
<a class="mailform" href="submit.php">Click</a>
我有这样的剧本,但它不工作:
$('.mailform').click(function() {
$.fancybox(
{
'autoDimensions' : false,
'width' : 350,
'height' : 'auto',
'transitionIn' : 'none',
'transitionOut' : 'none',
'ajax' : {
cache : false,
url : this.href
}
}
);
});
你能帮助我吗?
您需要返回false可以通过
$('.mailform').click(function() {
var myUrl = $(this).attr('href');
$.fancybox(
{
'autoDimensions' : false,
'width' : 350,
'height' : 'auto',
'transitionIn' : 'none',
'transitionOut' : 'none',
'ajax' : {
cache : false,
url : myUrl
}
}
);
return false;
});
当我这样做时,它显示此消息:http://prntscr.com/7qhh7 – vvhat 2012-04-04 12:05:12
我已更新它,以显示如何通过URL(您可能还需要传递一个完整的URL,不太确定如何fancybox想要它们) – BenOfTheNorth 2012-04-04 12:08:51
@BenGriffiths'ajax'对象是一个标准的'.ajax'对象,fancybox只是覆盖'success'和'error'处理程序。所以任何URL将与jQ工作应与fancybox – DaveRandom 2012-04-04 12:14:03
我怀疑你的问题在这里与魔术this
变量的范围有关。在您的代码中,this
指的是您已分配给'ajax':
的对象字面值。为了做我认为你正在尝试做的事情,你将需要另一个临时变量。按照惯例,我们称之为变量that
。您还需要从您的点击处理程序返回false
,停止浏览器跟随主窗口中的链接。
$('.mailform').click(function() {
// Here, `this` refers to the <a> DOM element, so we create a copy of it
// and store it in `that`
var that = this;
$.fancybox({
// Now `this` refers to the object you are passing to fancybox()
'autoDimensions': false,
'width' : 350,
'height' : 'auto',
'transitionIn' : 'none',
'transitionOut' : 'none',
'ajax' : {
// Now `this` refers to the object you are assigning to 'ajax':
cache : false,
url : that.href // Use `that` instead of `this`
}
});
// We also need to return false, to stop the browser following the link
return false;
});
Fancybox表示“请求的内容无法加载 请稍后再试” – vvhat 2012-04-04 12:09:02
查看您的Web服务器日志 - 您是否看到了带有错误代码结果的'submit.php'的请求? – DaveRandom 2012-04-04 12:12:48
你需要更具体的关于“不工作” – BenOfTheNorth 2012-04-04 11:47:34
应该在网页“submit.php”中的fancybox加载以下停止链接? – JFK 2012-04-04 16:16:14