javascript - 使用textareas在iframe中显示实时结果,使用jQuery,将单独的textarea输入添加到iframes head中

标签 javascript jquery html iframe textarea

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

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

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

谢谢大家的帮助。我有我的 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
            };
        });
    });
});

最佳答案

试试这个

   $(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
      };

      });
    });
  });

关于javascript - 使用textareas在iframe中显示实时结果,使用jQuery,将单独的textarea输入添加到iframes head中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22740665/

相关文章:

javascript - 按字母或数字字符对字符串数组进行排序

javascript - webpack.optimize.UglifyJSPlugin 不起作用,而 uglifyjs-webpack-plugin 起作用

javascript - 无法让我的方法连接 2 个值

c# - jquery .ajax 调用 Controller ,模型为空

html - Select2 宽度问题,控件覆盖父宽度

css - 我可以使用其所有 css 功能制作一个与其普通 View 相同的可打印网页吗?

javascript - 无法使用 node.js 和 express 从网页收集用户输入

javascript - Backbone.js 我们可以没有任何 tagName 吗?

javascript - 通过 Ajax 从 Django 连续获取图像

javascript - 在选项中解析JSON响应值