2016-11-23 46 views
-1

我想要替換iframe,如果用戶再次通過選擇不同的輸入點擊getPrice按鈕。我已經編寫代碼並嘗試了幾件事情,但沒有任何結果。每次我不能使用document.body.appendchild()。我的代碼如下。請在這裏建議。想要替換基於Onlclick的iframe

function getPrice()[ 
var ifrm = document.createElement('iframe'); 
    ifrm.id = "frameID"; 
ifrm.setAttribute('src', 'http://40.85.176.227/invoke/Tier1ICLStd_New.Price/getPrice?CloudName=Azure East 
ifrm.setAttribute('align', 'right'); 
//ifrm.setAttribute('marginheight', '50'); 
ifrm.style.margin= "auto"; 
ifrm.scrolling="no"; 
ifrm.style.border= "0"; 
ifrm.style.margin= "0"; 
//document.body.insertBefore(ifrm, document.body.firstChild); 
//alert(); 
if(document.getElementById("frameID")==null) 
{ 
document.body.appendChild(ifrm); 
} 
else 
{ 
alert("Else"); // I am getting control till here but what code to replace the existing iframe. 
//var item = document.getElementById("frameID").childNodes[0]; 
item.replaceChild(ifrm,item.childNodes[0]); 
alert("test"); 
    document.body.appendChild(ifrm); 

//ifrm.parentNode.replaceChild(ifrm, ifrm); 
} 
} 
+0

你有一個語法錯誤:'函數用getPrice()['應該是'函數用getPrice(){'' –

+0

ifrm.setAttribute( 'SRC',「HTTP ://40.85.176.227/invoke/Tier1ICLStd_New.Price/getPrice?CloudName = Azure East');'我想你的代碼是這樣的? –

+0

你想要替換完整的iframe或只是其內容? –

回答

0

如果你要更新的iframe中的內容,您只需要使用這樣的事情:

function updateIframeSrc(newSrc) { 
    document.getElementById("frameID").src = newSrc; 
} 

編輯:添加爲例與HTML聲明回答評論。

<div class="frameClass"> 
    <iframe id="frameID" width="100%" height="1080" frameborder="0" scrolling="auto" marginheight="0" marginwidth="0" 
     src="http://svn.min.not/metasite/trunk/docs/README.md"> 
    </iframe> 
</div> 
+0

document.getElementById(「iframeID」)。src ='http:// xyz';但沒有奏效。沒有發生任何事 – samash

+0

humm ...你的id是「frameID」,而不是「iframeID」...我編輯我的答案^^。 而這項工作,我完全使用它,因爲它是一些網頁。 –

+0

WOW !!!!!謝謝你,你抓住了它...我也早些時候嘗試過。但我想我沒有抓住這個....謝謝你。爲我工作......但不知何故,這不是在中心左或右...... – samash

0

沒有JQuery的:

var i = 0; 

var ifrm = document.createElement('iframe'); 
ifrm.id = "frameID"; 
ifrm.setAttribute('align', 'right'); 
ifrm.style.display="none"; 

document.body.appendChild(ifrm); 

function getPrice(){ 
    ifrm.setAttribute('src', 'http://40.85.176.227/invoke/Tier1ICLStd_New.Price/getPrice?CloudName=Azure East&token=' + i); 
    ifrm.style.display = "inline"; 
    i ++; 
} 
+0

我無法使用JQuery,因爲服務器由Cloud Provider維護。請提出替代方案。 – samash

+0

更新了我的答案 – Joe