JQuery - how to append object to iframe in WebKit

2019-02-19 10:45发布

问题:

I have this code:

  <script type="text/javascript">
    $(document).ready(function() {
        $("#iframeId").contents().find("body").append($("#test"));
    });
  </script>
  <iframe id="iframeId" name="iframeId" src="about:blank" ></iframe>
  <div id="test">
     Text
  </div>

And i need to append whole object to iframe (not only the html()). It works well in IE, Firefox and Opera, but I can't make it going in Chrome/Safari. Is there any hack or other way, how to put html object into the iframe, working with WebKit?

EDIT

I can't clone the object (or use its inner Html), because I need to use it with file input and I can't copy it due to security restrictions.

回答1:

outerHTML. This example works fine:

jQuery.fn.outerHTML = function() {
    return $('<div>').append( this.eq(0).clone() ).html();
};

$("#iframeId").contents().find("body").html($("#test").outerHTML());
$("#test").remove();

--EDIT

  <script type="text/javascript">
    $(document).ready(function() {
     $("#iframeId").contents().find("body").html($("<div></div>").append($("#test")).html());
    });
  </script>
  <iframe id="iframeId" name="iframeId" src="about:blank" ></iframe>
  <div id="test">
     Text
  </div>

--EDIT II

$("#iframeLast").contents().find("body").append($("<form></form>").append($("#inputLast")));