Javascript 希望基于单击替换iframe
如果用户通过选择不同的输入再次单击getPrice按钮,我想替换iframe。每次我无法使用document.body.appendchild()。我的代码如下。请在这里提出建议Javascript 希望基于单击替换iframe,javascript,iframe,Javascript,Iframe,如果用户通过选择不同的输入再次单击getPrice按钮,我想替换iframe。每次我无法使用document.body.appendchild()。我的代码如下。请在这里提出建议 function getPrice()[ var ifrm = document.createElement('iframe'); ifrm.id = "frameID"; ifrm.setAttribute('src', 'http://40.85.176.227/invoke/Tier1ICLStd_N
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);
}
}
如果要更新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>
不带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 ++;
}
您有一个语法错误:
函数getPrice()[
应该是函数getPrice(){
ifrm.setAttribute('src','http://40.85.176.227/invoke/Tier1ICLStd_New.Price/getPrice?CloudName=Azure East');
我想您的代码是这样的?是要替换整个Iframe还是只替换其内容?“在这里发布问题时只是输入错误。我想替换iframe。替换iframe和内容对我来说都是一样的,因为meI不能使用JQuery,因为服务器是由云提供商维护的。请建议替换。document.getElementById(“iframeID”).src='';但不起作用。没有发生任何事情嗯…您的id是“frameID”,而不是“iframeID”…我编辑我的答案^^。这部作品,我使用它就像它是某个网页一样。哇!!!!!谢谢你…你抓到了…我之前也试过了。但我想我没有抓到这个…非常感谢。对我来说也很有用…但不知怎的,这不是在中间,无论是左还是右…试着删除你的“对齐:右”如果你想让它居中。然后,我建议你在iframe上加一个width=“100%”属性并将其放置到div中。然后,将div放置在您想要的位置,这将更容易。现在只是一个CSS问题;)Javascript中div中的iframe?我在html正文中没有任何syntex。您能否在一行中提供一些示例…如何放置到div中?