doumibi6899 2019-04-06 11:20
浏览 255

如何使用jquery将HTML代码段附加到HTML iframe?

How to append the HTML code snippet to HTML iframe using jquery? Actually, I have added an HTML code snippet in MySQL Database using PHP. Now I have fetched that HTML Code snippet and trying to append that HTML code snippet in iframe using jquery.

$( document ).ready(function() {
    var snippets='<?php echo $snippets_preview; ?>';
    $(function() {  
        var $iframe = $('#iframehtml');
        $iframe.ready(function() {
            $iframe.contents().find("body").append(snippets);
        });
    });
});

But I am getting the following error

Uncaught SyntaxError: Invalid or unexpected token

snippets_preview has the following value in it. when I change the value of snippet_preview. it works fine.

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>Document</title>
        <link href="//maxcdn.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
        <script src="//maxcdn.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js"></script>
        <script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> 
        <style>'.$postdata['snippets_css'].'</style>
        <script>'.$postdata['snippets_javascript'].'</script>
    </head>
    <body>

        '.$ContentDecodedHTML.'

    </body>
</html>
  • 写回答

1条回答 默认 最新

  • douyiyang6317 2019-04-06 12:32
    关注

    If you want to embed html code in an iframe you have to do for example:

    JAVASCRIPT (class)

    <script>
        // -------------------------------------------------------- CLASS
            var class_iframe = {
                // ------------------------------------- STEP 1 (Create an new Iframe)
                    generateIframe: function(src, targetDOM){
                        // ---------------- Create iframe
                            var newIframe = document.createElement('iframe');
    
                            newIframe.onload = function(){
                                class_iframe.getIframe($('.iframehtml').contents());
                            };
    
                            newIframe.src = src;
                            newIframe.className = "iframehtml";
                            newIframe.frameBorder = "0";
    
                        // ---------------- Insert iframe in DOM
                            $(targetDOM).replaceWith(newIframe);
                    },
    
                // ------------------------------------- STEP 2 (Get iframe in this class)
                    getIframe: function(iframe){
                        class_iframe.frame = iframe; console.log(class_iframe.frame);
                    },
    
                // ------------------------------------- STEP 3 (Append in this iframe)
                    appendIframe: function(targetInIframe, content){
                        console.log(class_iframe.frame);
                        $target = class_iframe.frame.find(targetInIframe); console.log($target);
                        $target.prepend(content);
                    },
    
            }
    
        // -------------------------------------------------------- ACTION (loadPage)
            $(document).ready(function(){
                //$("body").prepend(`<div id="insertIframe"></div>`); //Just for the test
                class_iframe.generateIframe("your_url_iframe.html", "#insertIframe");
    
            });
    
    </script>
    

    In your html page

    <div id="insertIframe"></div>
    

    JAVASCRIPT (for append in your new iframe)

    <script>
        // -------------------------------------------------------- ACTION (after loadPage)
            class_iframe.appendIframe("html", `<div class="insert">Hello world !</div>`);
    </script>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 fesafe材料库问题
  • ¥35 beats蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统