2012-10-24 122 views
0

因此,我使用trigger.io創建了一個頁面,其中底部有一個自定義菜單,每個按鈕都將一個外部HTML頁面加載到主容器中。我不得不進行這項工作,所以我想知道是否有更好的方法。Trigger.io動態加載頁面

我開始使用$('.main').load('pages/test.html'),它不起作用。相反,我不得不這樣做:

forge.file.getLocal('pages/test.html', function (file) { 
    forge.file.string(file, function (str) { 
    $('.main').html(str); 
    }); 
}); 

這是有點麻煩。

另外,如果strHTML內容爲img標記,則img不會顯示,因爲src屬性會混亂。所以我不得不做另一個黑客:

forge.file.getLocal('pages/test.html', function (file) { 
    forge.file.string(file, function (str) { 
    var $main = $('.main'); 
    $main.html(str); 

    //Hack to resolve img src 
    var imgPath; 
    $main.find('img').each(function() { 
     var $this = $(this); 
     // First 8 chars is "file:///" 
     imgPath = $this.prop('src').substr(8); 
     forge.file.getLocal(imgPath, function (file) { 
     $this.prop('src', file.uri); 
     }); 
    }); 
    }); 
}); 

任何更好的方式純粹加載一個外部的HTML頁面沒有所有的麻煩?

謝謝!

回答

1

在Android 4.1和iOS(包括iPhone模擬器和iPad)上使用forge平臺v1.4(寫作時,v1.4.18)進行測試時,我似乎可以使用jQuery的加載方法,而無需任何額外的努力。下面是我的測試用例結構:

src/ 
    index.html 
    face.png 
    pages/ 
    hello.html 

這裏是index.html

<!DOCTYPE html> 
<html> 
<head> 
<script src="js/jquery-1.7.1.min.js"></script> 
<script type="text/javascript"> 
    $(function() { 
     $('.content').load('pages/hello.html'); 
    }); 
</script> 
</head> 
<body> 

<div class="content"> 
</div> 

</body> 
</html> 

而且pages/hello.html內容:

<b>hello world</b><img src="face.png"> 

剛剛應用程序啓動後,導致了這一點: Result of using $.fn.load

我可以用這種方法看到的一個問題是,img標記的src屬性必須相對於index.html。如果您仍然遇到問題,那麼使用更具體的測試用例和/或鍛造平臺版本的詳細信息以及您測試的設備/模擬器可能會有所幫助。