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