简体   繁体   English

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

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

Well, I am sort of asking a 2 part question but answering either part would help me out. 好吧,我有点想问一个分为两部分的问题,但是回答任何一部分都可以帮助我。 I will be direct in my question and skip straight to what I need help with. 我将直接回答我的问题,直接跳过我需要的帮助。 I am trying to use a Javascript function to define certain elements of an iframe so the user can decide the source, height, and width. 我正在尝试使用Javascript函数来定义iframe的某些元素,以便用户可以确定源,高度和宽度。 The code I have right now is as shown below. 我现在拥有的代码如下所示。

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

Is there a way i can reload the iFrame and have it use the user inputs as the elements with my current code? 有没有一种方法可以重新加载iFrame并将其使用用户输入作为当前代码的元素? Is there something else that i need to do to have the variables be used for the iFrame? 为了将变量用于iFrame,我还需要做其他事情吗? Thanks for anyone who looks at my question and looking forward to your answer. 感谢您关注我的问题并期待您的回答。

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

First initialize the IFrame with blank values and with one unique ID and then append user input. 首先使用空白值和一个唯一ID初始化IFrame,然后追加用户输入。

Here is the working JSFiddle Link 这是工作中的JSFiddle Link

Dynamically Changing Values: 动态更改值:

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

Note: You can also create IFrame element dynamically using createElement . 注意:您也可以使用createElement动态创建IFrame元素。 Check this out

Using Create Element: 使用创建元素:

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