2012-04-04 128 views
-2

我搜索了很多,但我找不到答案。如何使用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 
      } 
     } 
    ); 
}); 

你能幫助我嗎?

+0

你需要更具體的關於「不工作」 – BenOfTheNorth 2012-04-04 11:47:34

+0

應該在網頁「submit.php」中的fancybox加載以下停止鏈接? – JFK 2012-04-04 16:16:14

回答

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; 
}); 
+0

當我這樣做時,它顯示此消息:http://prntscr.com/7qhh7 – vvhat 2012-04-04 12:05:12

+0

我已更新它,以顯示如何通過URL(您可能還需要傳遞一個完整的URL,不太確定如何fancybox想要它們) – BenOfTheNorth 2012-04-04 12:08:51

+0

@BenGriffiths'ajax'對象是一個標準的'.ajax'對象,fancybox只是覆蓋'success'和'error'處理程序。所以任何URL將與jQ工作應與fancybox – DaveRandom 2012-04-04 12:14:03

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; 
}); 
+0

Fancybox表示「請求的內容無法加載 請稍後再試」 – vvhat 2012-04-04 12:09:02

+0

查看您的Web服務器日誌 - 您是否看到了帶有錯誤代碼結果的'submit.php'的請求? – DaveRandom 2012-04-04 12:12:48