简体   繁体   English

克隆,播放和替换克隆的Dom-Javascript / Jquery

[英]Clone, Play and Replace the Cloned Dom - Javascript / Jquery

I'm trying to export the whole page as PDF. 我正在尝试将整个页面导出为PDF。 During certain situation's like, if the CSS is loaded from separate file is not applied in exported PDF. 在某些情况下,如果CSS是从单独的文件加载的,则不会在导出的PDF中应用。 So I'm trying to convert all CSS as inline using this code. 因此,我正在尝试使用此代码将所有CSS转换为内联。

(function ($) {
  var rules = document.styleSheets;
  for(var rl in rules){
      var rule = rules[rl].cssRules;
      try{
      for (var idx = 0, len = rule.length; idx < len; idx++) {
        $(rule[idx].selectorText).each(function (i, elem) {
          if($(elem).is(":visible"))
            elem.style.cssText += rule[idx].style.cssText;
        });
      }
      }catch(e){
   console.log(e);
   }
   }

})(jQuery);

After I ran this code, my exported PDF is working good. 运行此代码后,导出的PDF正常运行。 But my DOM is not as before. 但是我的DOM不像以前那样。 So is there anyway where I can clone my DOM before operations, and replace the cloned DOM as before after playing with DOM. 无论如何,我可以在进行操作之前克隆DOM,并在玩DOM之后像以前一样替换克隆的DOM。 Hope my question is clear. 希望我的问题清楚。 Thanks in anticipation for the help. 感谢您的帮助。

In this Snippet there are 2 much more simpler ways than modifying a stylesheet: 在此摘录中,有两种比修改样式表简单得多的方法:

  1. Isolate the <iframe> , <embed> , or <object> by wrapping an element around it then apply styles referencing the wrapper element. 通过在元素周围包裹元素来隔离<iframe><embed><object> ,然后应用引用包装元素的样式。 This is demonstrated in the Snippet with div.jframe as the wrapper. 这在代码段中以div.jframe作为包装器进行了演示。
  2. Inject a <style> block with new rulesets. 为新<style>集注入<style>块。

If either one is done with moderate care, you shouldn't be left with conflicting styles. 如果其中任何一项都经过适当的护理,就不应该让样式冲突。

Note: The PDF in the iframe is sandboxed, so it's not there but everything still applies. 注意: iframe中的PDF已被沙箱化,因此它不存在,但一切仍然适用。

SNIPPET SNIPPET

 function injectStyles(rule) { document.body.insertAdjacentHTML('afterbegin', '&shy;<style>' + rule + '</style>'); } injectStyles('iframe:hover { border: 5px solid blue; }'); 
 .jframe iframe { outline: 10px solid tomato; } 
 <div class='jframe'> <iframe src='http://che.org.il/wp-content/uploads/2016/12/pdf-sample.pdf' height='400' width='400'></iframe> </div> 

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

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