Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
219 views
in Technique[技术] by (71.8m points)

javascript - wants to replace the iframe based on Onlclick

I wants to replace the iframe, if user clicks on getPrice button again by selecting different input.I have written code and tried several things but nothing worked out. everytime I cant use document.body.appendchild(). My code is below. Please suggest here.

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);
}
} 
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you want to update the iframe content, you just need to use something like this :

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

EDIT : Add exemple with HTML declaration to answer a comment.

<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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...