如何用fancybox加载php页面?

时间:2012-04-04 11:42:56

标签: php fancybox

我搜索了很多,但我找不到答案。

在我的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
             }
        }
    );
});

你能帮我吗?

2 个答案:

答案 0 :(得分:1)

我怀疑你的问题与魔法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;
});

答案 1 :(得分:1)

您需要返回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;
});
相关问题