简体   繁体   English

如何同时检索HTML和页面部分的样式?

[英]How to retrieve both html and styles of a page section?

I am trying to retrieve the current content of a Web page section (usually a div or table element). 我正在尝试检索网页部分(通常是div或表元素)的当前内容。 The objective is to display it as standalone (for example for printing purposes) or insert it in another page. 目的是将其独立显示(例如出于打印目的)或将其插入另一个页面。 It is essential for me to collect not only the elements but also their computed styles, as some content has been highlighted or color coded by other scripts. 对于我来说,不仅要收集元素,而且要收集它们的计算样式,这是至关重要的,因为某些内容已被其他脚本突出显示或用颜色编码。

I can get the html with innerHTML, and get the style of a specific element with getComputedStyle. 我可以使用innerHTML获取html,并使用getComputedStyle获取特定元素的样式。 But how would I get both html and styles for a whole section? 但是,如何在整个部分中同时获取html和样式?

I once made something in jQuery which could retrieve ALL (not specific parts) the styles; 我曾经在jQuery中做过一些事情,可以检索所有(不是特定部分)样式。 .css files, style-tags and inner-styles of a certain page. .css文件,特定页面的样式标签和内部样式。 I didn't know what to do with it but maybe you can use this script somehow. 我不知道该怎么办,但也许您可以以某种方式使用此脚本。 It doesn't work in all scenarios and sometimes it will be a little buggy because I haven't tested it enough. 它并非在所有情况下都适用,有时会有点bug,因为我还没有对其进行足够的测试。 Maybe there are some other people on StackOverflow that can continue on with this piece of code. 也许StackOverflow上还有其他人可以继续这段代码。 ;) ;)

var all_css = {stylesheets:[], inner_head_styles:[], body_styles:[]};

$(function(){
  $.ajaxSetup({ 
    async: true,
    crossDomain: true,
    dataType: "text",
    type: "GET"
  });

  var calls = [];
  /*every non-cross-domain .css file*/
  $("head link[rel='stylesheet']").each(function(a, stylesheet){
    if($(stylesheet).attr("href").match(/((ftp|http|https):\/\/)?/)[0] == ""){
      var css_source = $(stylesheet).attr("href");
      var css_call = $.ajax({
        url: css_source,
        success: function(data){
          all_css.stylesheets.push(data);
        },
        error: function(e, f){
          alert(e, f);
        }
      });

      calls.push(css_call);
    }
    else{
      console.log("CSS SOLVER: Cross domain CSS's aren't going to be loaded!");
    }
  });

  /*every head style*/
  $("style").each(function(b, style){
    all_css.inner_head_styles.push($(this).text());
  });

  /*every element in the body that has a style attribute*/
  $("body[style], body *[style]").each(function(c, style){
    var css_html_node = $(style).context.nodeName;
    var css_class = typeof($(style).attr("class")) != "undefined" ? "."+$(style).attr("class") : "";
    var css_id = typeof($(style).attr("id")) != "undefined" ? "#"+$(style).attr("id") : "";

    css_class = css_class.replace(/\s/g, ".");
    var css_string = css_html_node + css_id + css_class + "{" + $(style).attr("style") + "}";
    all_css.body_styles.push(css_string);
  });


  $.when.apply(null, calls).done(function(){
    console.log(all_css);
  });
});

You can't really retrieve the styles for a section, you have to get the styles on an element. 您实际上无法检索节的样式,而必须在元素上获取样式。

You have to bear in mind that you aren't dealing with the original CSS any longer, but the calculated style for each element. 您必须记住,您不再处理原始CSS,而是处理每个元素的计算样式。

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

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