2016-08-11 20 views
0

我正在加載一個完整的html電子郵件(打開和關閉html標籤)到我的模態用於預覽目的。就像這樣:如何防止在我的引導模式下加載的html影響我的頁面html?

//Update modal body 
$('.modal-body').html(response['html']); 

我遇到的問題是,當我這樣做,加載HTML似乎(不出所料)改變模式之外的主要頁面內容。

我想知道的是,是否有辦法將html隔離爲模態區域,以便它不會影響主頁的html。

我已經研究了使用iframe作爲替代方案,但對我來說問題是我通過ajax(以及其他數據)加載此數據,並且iframe似乎只支持通過鏈接提取信息在飛行中。

我很感謝這裏的任何潛在解決方案。

謝謝!

+0

可能的重複:http://stackoverflow.com/questions/16504816/how-to-set-html-content-into-an-iframe和/或http://stackoverflow.com/questions/8226307/set -iframe-innerhtml-without-loading-page-in-it-with-jquery – JonSG

+0

這些解決方案在我的結尾不起作用。 iframe似乎沒有加載必要的html。 – Tapha

回答

0

這些可能無法在這裏工作,無論是在哪裏沙盒,但是,這對我本地工作。

var target = document.getElementById('target'); 
 
var targetDoc = target.contentWindow.document; 
 

 
targetDoc.open('text/htmlreplace'); 
 
targetDoc.write("<html><body>Hello world</body></html>"); 
 
targetDoc.close();
<iframe id="target"></iframe>

一樣:

var target = document.getElementById('target'); 
 
target.src = "javascript:'<html><body>Hello world</body></html>'";
<iframe id="target"></iframe>

雖然我認識的第二個例子是尷尬的明智管理的報價。

這是一個完整的工作引導示例。那麼它在本地工作,但再次,不在這個沙箱。

var target = document.getElementById('target'); 
 
var targetDoc = target.contentWindow.document; 
 

 
targetDoc.open('text/htmlreplace'); 
 
targetDoc.write("<html><body>Hello world</body></html>"); 
 
targetDoc.close();
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous"> 
 

 

 
<!-- Button trigger modal --> 
 
<button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal"> 
 
    Launch demo modal 
 
</button> 
 

 
<!-- Modal --> 
 
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"> 
 
    <div class="modal-dialog" role="document"> 
 
    <div class="modal-content"> 
 
     <div class="modal-header"> 
 
     <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button> 
 
     <h4 class="modal-title" id="myModalLabel">Modal title</h4> 
 
     </div> 
 
     <div class="modal-body"> 
 

 

 
     <iframe id="target"></iframe> 
 

 

 
     </div> 
 
     <div class="modal-footer"> 
 
     <button type="button" class="btn btn-default" data-dismiss="modal">Close</button> 
 
     </div> 
 
    </div> 
 
    </div> 
 
</div> 
 

 

 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>

雖然它在沙箱中失敗,使這樣的地方......

enter image description here

+0

問題出在這裏(我剛剛測試過),當加載數據html時,主html仍然被改變。 – Tapha

+0

你的模態主體應該只是靜態** iframe **在這一點上?您不再更新模式正文,而是在正文中設置靜態iframe。 – JonSG

0

Tapha:

相反的jQuery,你可以試試以下代碼:

<div> 
    <object type="text/html" data="http://www.google.com/" width="800px" height="600px" style="overflow:auto;border:5px ridge blue"> 
    </object> 
</div> 

希望這有助於!

相關問題