【问题标题】:wants to replace the iframe based on Onlclick想替换基于Onclick的iframe
【发布时间】:2016-11-23 10:02:06
【问题描述】:

如果用户通过选择不同的输入再次单击 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_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);
}
} 

【问题讨论】:

  • 你有一个语法错误:function getPrice()[ 应该是function getPrice(){
  • ifrm.setAttribute('src', 'http://40.85.176.227/invoke/Tier1ICLStd_New.Price/getPrice?CloudName=Azure East');我猜你的代码是这样的?
  • 您要替换完整的 iframe 还是仅替换其内容?
  • "[" 在这里发布问题时只是拼写错误。我想替换 iframe。替换 iframe 和内容对我来说都是一样的,两者都对我有用

标签: javascript iframe


【解决方案1】:

如果你想更新 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>

【讨论】:

  • document.getElementById("iframeID").src = 'xyz';但没有奏效。什么都没发生
  • 嗯...您的身份是“frameID”,而不是“iframeID”...我编辑我的答案^^。而这项工作,我完全按照网页的方式使用它。
  • 哇!!!!谢谢你..你抓住了它......我早些时候也试过这个。但我想我没有抓住这个......非常感谢。也为我工作......但不知何故这不是在左边或右边......
  • 如果您希望它居中,请尝试删除您的“align:right”。然后,我建议您将 iframe 与 width="100%" 属性一起放入 div 中。然后,将你的 div 放在你想要的地方,它会更容易。现在只是一个 CSS 问题 ;)
  • Javascript 中 div 中的 iframe?我在这里的 html 正文中没有任何语法。您能否在一行中提供示例...如何放入 div 中?
【解决方案2】:

没有 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 ++;
}

【讨论】:

  • 我不能使用 JQuery,因为服务器是由 Cloud Provider 维护的。请提出替代方案。
猜你喜欢
  • 2010-11-30
  • 1970-01-01
  • 1970-01-01
  • 2013-07-04
  • 2012-08-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多