【问题标题】:is there a package that turns inline styles to css classes是否有将内联样式转换为 css 类的包
【发布时间】:2020-01-26 17:20:44
【问题描述】:

如果我通过了有没有一个包

<p style="width: 500px; height: 500px"> Hello World </p>

将返回

<p class="foo"> Hello World </p>
.foo {
  width: 500px;
  height: 500px;
}

【问题讨论】:

标签: html css npm


【解决方案1】:

使用 JS 来做到这一点。用样式标签抓取所有元素

var els = document.querySelectorAll('[style]');

遍历所有元素,给它们一个随机的类,并在样式标签中添加语法

for (i = 0; i < els.length; i++) {
  var rndClass = getUniqueRandomClass();

  var style = els[i].style;
  var returnStyle = "." + rndClass + "{" + style + "}";
  document.querySelector('style').textContent += returnStyle;

  els[i].classList.add(rndClass);
  els[i].removeAttribute("style");
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-06-26
    • 2014-09-25
    • 1970-01-01
    • 1970-01-01
    • 2015-04-20
    • 1970-01-01
    • 2016-01-01
    • 1970-01-01
    相关资源
    最近更新 更多