2011-11-16 69 views
0

我使用simple modal來了解jQuery模式如何工作。通過該處理在jQuery模式中顯示外部文件的內容

var load = 'alert.html'; // THE PURPOSE OF THIS QUESTION IS TO CHANGE "alert.html" to "image.jpg" 
$(this).click(function(e) { 
    e.preventDefault(); 
    $('body').append('<div id="overlay" />'); 
    $('#overlay').fadeIn(300, function() { 
     $('body').append('<div id="alertModalOuter"><div id="alertModal"></div></div>'); 
     var outer = $('#alertModalOuter'); 
     var modal = $('#alertModal'); 
     var defWidth = outer.outerWidth(); 
     var defHeight = outer.outerHeight(); 
     modal.load(load + ' #alert', function() { 
      var alertBoxContent = $('#alert'); 
      var alertWidth = alertBoxContent.outerWidth(); 
      var alertHeight = alertBoxContent.outerHeight(); 
      var widthCombine = -((defWidth + alertWidth)/2); 
      var heightCombine = -((defHeight + alertHeight)/2); 
      modal.animate({width: alertWidth, height: alertHeight}, 200); 
      outer.animate({marginLeft: widthCombine, marginTop: heightCombine}, 200, function() { 
       alertBoxContent.fadeIn(200, function() { 
       }); 
      }); 
     }); 

這附加的外部文件來(從load)到模式窗口的內容;但這隻適用於id =「alert」標籤內的內容。如何刪除「警報」角色以顯示外部文件的全部內容。例如,我想加載一個外部圖像(這是一個圖像文件,而不是「alert」標籤)。

回答

1

您不必指定#alert選擇器,它將加載整個頁面。值得注意的是,如果您不指定選擇器,則在刪除它們之前加載調用.html()並處理所有腳本。您可能正在運行一些腳本,從而爲您提供意外的結果The .load() docs

+0

你的意思是簡單地用'modal.load(load,function(){var alertBoxContent = $();'''修改代碼這是我做的第一件事,但沒有奏效! – Googlebot