2013-10-29 42 views
0

我有一個網頁上的動態表,我打電話給另一個應用程序的頁面。我試圖單獨顯示該表而不是整個頁面。所以我寫了一個基於堆棧交換的其他答案的代碼。但它仍然顯示整個頁面。如果表格只有2行,那麼它將顯示具有2行的表格和整個空白頁面。無法弄清楚什麼是錯的。有沒有其他方法?使用iframe顯示網頁的一部分

<script language="javascript" type="text/javascript"> 
function resizeIframe(obj) { 
obj.style.height = obj.contentWindow.document.getElementById("dvMain").offsetHeight + 'px'; 
// alert(obj.style.height); 
} 
</script> 


<iframe src="www.google.com" width="100%" marginwidth="0" marginheight="0" frameBorder="0" id="iframe" onload='javascript:resizeIframe(this);'> 
</iframe> 
+0

如果這兩個網頁在同一臺服務器上,你可以使用XMLHttpRequest加載文本響應和 使用從<表位的div元素的innerHTML。如果它們的來源不同,則無法讀取contentWindow屬性。 – kennebec

+0

@kennebec:謝謝你的迴應。你能不能展示代碼大綱?這些頁面將位於同一臺服務器上。 – Ann

回答

0
function tableFromUrl(url, target, before){ 
    url is the file path, 
    target is an element to append the table to, if there are two arguments. 
    Or, if a third argument is true, insert the table before the target element. 
    try{ 
     var O= new XMLHttpRequest(), txt, 
     div= document.createElement('div'); 
     O.open('GET', url, true); 
     O.onreadystatechange= function(){ 
      if(O.readyState== 4 && O.status== 200){ 
       txt= O.responseText; 
       div.innerHTML=txt.substring(t.search(/<table/), t.search(/<\/table>/))+'</table>'; 
       if(before=== true) target.parentNode.insertBefore(div, target); 
       else target.appendChild(div); 
      } 
     } 
     O.send(null); 
    } 
    catch(er){ 
     //error handler 
    } 

} 
+0

再次感謝您的回覆。稍作修改後,我終於用javascript獲得了期望的結果。 – Ann

相關問題