繁体   English   中英

使用Java脚本功能重新加载HTML元素?

[英]Reload an HTML element using a java script function?

好吧,我有点想问一个分为两部分的问题,但是回答任何一部分都可以帮助我。 我将直接回答我的问题,直接跳过我需要的帮助。 我正在尝试使用Javascript函数来定义iframe的某些元素,以便用户可以确定源,高度和宽度。 我现在拥有的代码如下所示。

 <script language="javascript"> window.onload = webPage; function webPage(){ var page = prompt("please enter the website","www.google.com"); var height = prompt("Please enter the height of the frame","800"); var width = prompt("Please enter the width of the frame","600"); } </script> 
 <iframe src=page height=height width=width> </iframe> 

有没有一种方法可以重新加载iFrame并将其使用用户输入作为当前代码的元素? 为了将变量用于iFrame,我还需要做其他事情吗? 感谢您关注我的问题并期待您的回答。

 <script language="javascript"> window.onload = webPage; function webPage(){ var page = prompt("please enter the website","www.google.com"); var height = prompt("Please enter the height of the frame","800"); var width = prompt("Please enter the width of the frame","600"); //new content here document.getElementById("theIframe").innerHTML="<iframe src=\\""+page+"\\" height=\\""+height+"\\" width=\\""+width+"\\"></iframe>"; } </script> 
 <div id="theIframe"></div> 

首先使用空白值和一个唯一ID初始化IFrame,然后追加用户输入。

这是工作中的JSFiddle Link

动态更改值:

<!DOCTYPE html>
<html>
<head>
    <title>Demo</title>
</head>
<body>
<iframe id="icereaper666" src="" height="0" width="0"></iframe>
<script language="javascript">
window.onload = webPage;
function webPage(){
    var page = prompt("please enter the website","http://www.w3schools.com/");
    var height = prompt("Please enter the height of the frame","400");
    var width = prompt("Please enter the width of the frame","400");
    var iframeElement = document.getElementById("icereaper666");
    iframeElement.src = page;
    iframeElement.height = height;
    iframeElement.width = width;
}
</script>
</body>
</html> 

注意:您也可以使用createElement动态创建IFrame元素。 Check this out

使用创建元素:

<!DOCTYPE html>
<html>
<head>
    <title>Demo</title>
</head>
<body>
<script language="javascript">
    window.onload = webPage;
    function webPage(){
        var page = prompt("please enter the website","http://www.w3schools.com/");
        var height = prompt("Please enter the height of the frame","400");
        var width = prompt("Please enter the width of the frame","400");
        var iframeElement = document.createElement("iframe");
        iframeElement.src = page;
        iframeElement.height = height;
        iframeElement.width = width;
        document.body.appendChild(iframeElement);
    }
</script>
</body>
</html>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM