【问题标题】:Using textareas to display live results in iframe, using jQuery, add separate textarea input into iframes head使用 textarea 在 iframe 中显示实时结果,使用 jQuery,将单独的 textarea 输入添加到 iframe 头部
【发布时间】:2014-05-09 13:23:19
【问题描述】:

我创建了两个文本区域。一种用于 HTML,一种用于 CSS 输入。每个人都有自己的 ID。使用我在网上找到的教程,我能够让这些文本区域接受用户输入并将其实时显示在 iFrame 中。

然后我能够编写一些 jQuery,它从文本区域获取用户输入,ID 为 HTML,并将其添加到 iFrames BODY 标签,因此在 iFrame 中模拟 HTML 作为实时预览。此外,jQuery 使用 boolean 来检测文本区域中是否有用户输入而没有 html 的 ID(在本例中是 ID 为 css 的文本区域),然后将输入插入到 STYLE 标记内的 iFrame 的 HEAD ,因此将 CSS 添加到 iframe 的头部。在它自己的样式标签中,并允许用户在 iFrame 内生成其 CSS 和 HTML 的实时输出。一切都很好,坚如磐石。我能够使用文本字段在我眼前生成实时 HTML 和 CSS 结果。

我的问题是,我需要在下面的 jQuery 代码中添加什么,以允许将来自具有标题链接 ID 的单独文本区域的输入发送到 iFrames HEAD?我想要带有头链接 ID 的文本区域,将用户输入发送到 iframe 的 HEAD 这将允许该工具的用户链接到他们自己的外部样式表和/或 jquery/javascript 库等。

感谢大家的帮助。我有我的 jQuery 脚本。请告诉我你的想法。

回答:使用 jQuerys .clone 方法将父文档头部的 jQuery LINKS 和 SCRIPT 标签添加到 iFrames 头部更容易。下面是工作代码。

$(document).ready(function() 
{ 
    // .Grid Window Height
    $('.grid').height( $(window).height() );
    // Declaring the Output Window Variables
    var frame = $('iframe'); // The iFrame variable itself
    contents = frame.contents(); // Declares the variable of contents which is the iFrames content
    body = contents.find('body'); //body variable finds the <BODY></BODY> tags in the iFrame
    contents.find('head') // .find the HEAD...
        .append('<style type=text/css></style>') // then, add a <STYLE></STYLE> tag to it...
    styleTag = contents.find("style");  

    // Append Parent Document HEAD Elements with the class of wst to the iFrames HEAD          
    var jq = $(".wst").clone();
    frame.contents()
        .find("head")
        .append(jq);

    // Detect textarea KeyUp during focus
    $('textarea').on("focus", function(e) 
    {
        var $this = $(e.target);

        $(document).keyup(function() 
        {
            // If the ID of HTML is found, inster the HTML into the iFrame's <BODY></BODY> tags
            if ( $this.attr('id') === 'html') 
            {
                body.html( $this.val() ); // Insert current value into the iFrames <BODY></BODY> tags
            }; 

            if ( $this.attr('id') === 'css') 
            {
                // Else the ID of HTML is not found...

                styleTag.text( $this.val() ); // Insert CSS into styleTag variable
            };
        });
    });
});

【问题讨论】:

    标签: javascript jquery html iframe textarea


    【解决方案1】:

    试试这个

       $(document).ready(function () { //DOC Ready, start of script
          // .Grid Window Height
          $('.grid').height($(window).height());
          // Declaring the Output Window Variables
          var frame = $('iframe'), // The iFrame variable itself
              contents = frame.contents(), // Declares the variable of contents which is the iFrames content
              body = contents.find('body'), //body variable finds the <BODY></BODY> tags in the iFrame
              styleTag = contents // styleTag variable says to...
          .find('head') // ...find the HEAD of the iframe...
          .append('<style></style>'); // ...then, add a <STYLE></STYLE> tag to it.
    
          var scripts = "<script id=jquerycore type=text/javascript src=http://code.jquery.com/jquery-1.11.0.min.js></script>"+"<br />"+
                        +"<script id=jqueryuicss type=text/javascript src=http://code.jquery.com/ui/1.10.4/jquery-ui.min.js></script>"+"<br />"+
                        +"<script id=fontawesome type=text/javascript src=//netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.min.css></script>"+"<br />";
                contents.find("head").append(scripts);
    
    
          // Detect textarea KeyUp during focus
    
      $('textarea').focus(function () {
        var $this = $(this);
    
        $this.keyup(function () {
          // If a user inputs data into the textarea with an ID of HTML, insert that input into the iFrame's <BODY></BODY> tags
          if ($this.attr('id') === 'html') {
            body.html($this.val()); // Inster current value into the iFrames <BODY></BODY> tags
          };
          if ($this.attr('id') === 'css') {
            // Else the ID of HTML is not found...
            styleTag.text($this.val()); // ...Insert user input into the iFrames HEAD >> SCRIPT tags, via the styleTag variable
          };
    
          });
        });
      });
    

    【讨论】:

    • 这会将文本区域中的相应代码添加到 HEAD 中,还是添加到 HEAD 的 LINK 标记中?因为我试过了,它对我不起作用。我使用您的建议尝试使用的新代码更新了我的帖子,对吗?
    • @user3267537 该作品未经测试。在link id 上尝试regex 以获得多个预期链接。也许尝试使用单个 id 插入 head 和 if 语句。此外,更新n 可能需要额外的逻辑。可以创建一个jsfiddle,发布html 吗?如果解释正确,要求插入 link src attribute, url ?尝试将 input 映射到 link src 进行测试?再次,未经测试。已使用的模式已经存在于已发布的文章中,如插入的 style 元素、keyup event 以类似的方式存在。感谢分享。如果有能力很乐意提供帮助。希望这会有所帮助
    • @user3267537 请查看更新后的帖子。感谢分享。希望这会有所帮助
    • 感谢您的建议,但我更愿意根据自己的要求使用现有的 JS,而不是其他人。不过还是谢谢你。 .另外,我想了一会儿。我的目标是在 iFrames HEAD 中预定义 LINK 和 SCRIPT 标签。为了将链接附加到最新的 jQuery UI、jQuery UI CSS、jQuery 库和 Font Awesome CSS,我必须向现有的 JS 添加什么。这样,当 iframe 在浏览器中加载时,它会应用那些 LINK 和 SCRIPT 标签,从而允许用户为它们加载库。提前感谢一切=)
    • @user3267537 请查看更新后的帖子。查了jQuery UI CSS,没找到,和api.jqueryui.com/theming/css-framework一样吗?包含在jquery ui 中?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多