Generating a stylesheet based on inline HTML styling?

前端 未结 3 1931
有刺的猬
有刺的猬 2021-01-27 02:03

I\'ve been working on styling different parts of my website for a while, however I have yet to put my inline styles into a stylesheet. I was wondering if a tool exists to parse

3条回答
  •  天命终不由人
    2021-01-27 02:27

    Here, I wrote a function to do it (the specificity won't be perfect, but it'll get you started):

    function getInlineStyles() {
      var stylesList = "",
          thisElement,
          style,
          className,
          id;
      $("*", "body").each(function () {
        thisElement = $(this);
        style = thisElement.attr("style");
        className = thisElement.attr("class");
        id = thisElement.attr("id");
        if (id !== undefined) {
          stylesList += " #" + id;
        }
        if (className !== undefined) {
          stylesList += " ." + className;
        }
        if (id !== undefined || className !== undefined) {
          stylesList += "{";
        }  
        if (style !== undefined) {
          stylesList += style;
        }
        if (id !== undefined || className !== undefined) {
          stylesList += "}";
        }
      });
      return stylesList;
    }
    

提交回复
热议问题