javascript 如何在单击按钮时显示iframe

xa9qqrwz  于 2023-01-11  发布在  Java
关注(0)|答案(4)|浏览(258)

我只想知道如何在单击按钮时显示iframe
下面是我的代码:

function postYourAdd () {
    var iframe = $("#forPostyouradd");
    iframe.attr("src", iframe.data("src")); 
}
<button id="postYourAdd" onclick="postYourAdd()">Button</button>
<iframe id="forPostyouradd" data-src="http://www.w3schools.com" src="about:blank" width="200" height="200" style="background:#ffffff"></iframe>
slmsl1lt

slmsl1lt1#

至少在您提供的Snippet中,您忘记了添加对JQuery的引用。

function postYourAdd () {
    var iframe = $("#forPostyouradd");
    iframe.attr("src", iframe.data("src")); 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<button id="postYourAdd" onclick="postYourAdd()">OPEN</button>
<iframe id="forPostyouradd" data-src="http://www.w3schools.com" src="about:blank" width="500" height="200" style="background:#ffffff"></iframe>
r8uurelv

r8uurelv2#

<body>
<p>This is IFrame</p>
    <button onclick="displayIframe()">Click me</button>
  <div id="iframeDisplay"></div>  

<script>
    function displayIframe() {
        document.getElementById("iframeDisplay").innerHTML = "<iframe src=\"../HtmlPage1.html\" height=\"200\" width=\"300\" ></iframe>";

    }
</script>
</body>
6rqinv9w

6rqinv9w3#

function postYourAdd () {
    var iframe = $("#forPostyouradd");
    iframe.attr("src", iframe.data("https://www.google.com/")); 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<button id="postYourAdd" onclick="postYourAdd()">OPEN</button>
<iframe id="forPostyouradd" data-src="http://www.w3schools.com" src="about:blank" width="500" height="200" style="background:#ffffff"></iframe>
zvms9eto

zvms9eto4#

<html>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<body>

<button id="postYourAdd" onclick="light()">OPEN</button>
<iframe id="forPostyouradd" src="" title="W3Schools Free Online Web Tutorials">
</iframe>

<script>
function light() {
    var iframe = $("#forPostyouradd");
    debugger;
    iframe.attr("src", "https://www.w3schools.com/java/default.asp"); 
}
</script>
</body>
</html>

相关问题