是否有自动匹配类名称的工具?

时间:2016-12-25 09:36:57

标签: javascript css

是否有自动匹配类名称的工具?

如果给出HTML文件和CSS文件

示例:

HTML文件

<div class="a"></div>

CSS文件

.a {
  font-size: 12px;
  color: blue;
}

生成如:

{
  a: {
    'font-size': '12px',
    'color': 'blue',
  }
}

1 个答案:

答案 0 :(得分:0)

DIY是一个选择:

&#13;
&#13;
appendCss({
  "my-class": {
    "background": "yellow",
    "padding": "10px"
  }
});

function appendCss (css) {
  var style = document.createElement("style");
  style.textContent = cssToString(css);
  document.head.appendChild(style);
}

function cssToString (css) {
  var c, p, s = "";
  for (c in css) {
    s += "." + c + "{";
    for (p in css[c]) {
      s += p + ":" + css[c][p] + ";";
    }
    s += "}";
  }
  return s;
}
&#13;
<span class="my-class">merry xmas</span>
&#13;
&#13;
&#13;