简体   繁体   English

jQuery将脚本和HTML插入iframe

[英]JQuery Insert Script & HTML into Iframe

I'm trying to insert a Script & HTML into an iframe. 我正在尝试将脚本和HTML插入iframe。 My issue is I can append the HTML to the iframe, but I cannot append it and also keep the source of the Iframe present. 我的问题是我可以将HTML附加到iframe,但不能附加HTML,也不能使iframe的源代码存在。

The ultimate goal is to load HTML into the iframe source and have an overlay. 最终目标是将HTML加载到iframe源中并进行覆盖。 Please advise. 请指教。 I think I'm a little off on this one. 我想我对此还差一点。

$(function() {
            var $frame = $('<iframe class="viewTrackingFrame" id="viewframe" src="https://www.google.com" width="100%" height="100%" style="width:100%; height:100%;position:relative;">');
            $('body').html( $frame );
            setTimeout( function() {
                var doc = $frame[0].contentWindow.document;
                var $body = $('body',doc);
                $body.html('<h1>Test</h1>');
                $body.html('<div id="cursor" style="position:absolute;style:z-index:999999;"><img width="80" src="/assets/img/cursor_blue.png" /></div>');
                $body.html('<p id="test" style="z-index:9999999;">The queue length is: <span></span></p>');

            }, 1 );
        });

Here is what you can do. 这是您可以做的。 Create an iframe with empty src attribute. 创建一个具有空src属性的iframe。 Give it some id. 给它一些ID。 Make a ajax call to required url to fetch all the html and append it to iframe's body. 对所需的网址进行ajax调用,以获取所有html并将其附加到iframe的正文中。 Now append any additional HTML that you wish to add in the same iframe. 现在,将要添加到同一iframe中的所有其他HTML追加。 I have tested the method in a jsfiddle, check out http://jsfiddle.net/9vhL2/ 我已经在jsfiddle中测试了该方法,请查看http://jsfiddle.net/9vhL2/

$(function() {
    var $frame = $('<iframe class="viewTrackingFrame" id="viewframe" src="" width="100%" height="100%" style="width:100%; height:100%;position:relative;">');
            $('body').html( $frame );
                 var $body = $("#viewframe").contents().find('body');
            $.ajax( 
                {
                    url:"/echo/html/",
                    method:'POST',
                    success:function(data){
                      $body.append(data);
                      $body.append("<h1>Text added by script</h1>")
                      },
                    data: {html: "<p>Text echoed back to request</p><h2>Text from page</h2>"}
                });
});

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

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