开发者

Is it possible to create an iframe on click of a button using javascript

开发者 https://www.devze.com 2023-02-17 03:52 出处:网络
I have a UI with a print button, i want to create an iframe on click of the print button, is it possible us开发者_开发问答ing javascript?document.getElementById(\'print-button\').onclick = function()

I have a UI with a print button, i want to create an iframe on click of the print button, is it possible us开发者_开发问答ing javascript?


document.getElementById('print-button').onclick = function() {

   var iframe = document.createElement('iframe');
   iframe.src = 'http://example.com';
   document.body.appendChild(iframe);

};

Of course, if you're intending to attach more events of the same type, use addEventListener().

If jQuery is at your disposal...

$("#print-button").click(function() {
    $("<iframe />", { src: "http://example.com" }).appendTo("body");
});


Sure...#printbutton is the ID of your print button.

<a href="#" id="printbutton">Print</a>
<div id="iframe_parent"></div>

<script src="PATH-to-JQUERY"></script>
<script>
$('#printbutton').click(function(){
var iframe = document.createElement('iframe');
iframe.setAttribute('src', 'path/to/iframe/page/');
document.getElementById('iframe_parent').appendChild(iframe);
});
</script>


<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.0/jquery.min.js"></script>
<script type="text/javascript">
$(function(){
    $('#button').click(function(){
        if(!$('#iframe').length) {
                $('#iframeHolder').html('<iframe id="iframe" src="http://w3schools.com" width="700" height="450"></iframe>');
        }
    });   
});
</script>

<a id="button">Button</a>
<div id="iframeHolder"></div>
0

精彩评论

暂无评论...
验证码 换一张
取 消