我知道我可以使用window.getComputedStyle(el)
在浏览器DOM中获得元素的计算样式。
但是,是否存在与名称document.resolveAllClassRulesToInlineStyles()
更匹配的内容,使我能够按照名称对JavaScript内存文档的含义进行操作。
输入:
.warning {
color: red;
}
<html>
<div style="color: blue;">Blue</div>
<div class="warning">Red</div>
</html>
输出:
<html>
<div style="color: blue;">Blue</div>
<div style="color: red;">Red</div>
</html>
我认为JS很难实现html的内联样式。
但你也可以试试那些在线工具来做同样的事情。
Premailer.io
活动监视器CSS Inliner
响应式电子邮件CSS Inliner
Mailchimp CSS Inliner工具
<!-- Test Example -->
<html>
<div style="color: blue;">Blue</div>
<div class="warning">Red</div>
</html>
<style>
.warning {
color: red;
}
</style>
<!-- Output -->
<html>
<div style="color: blue;">Blue</div>
<div class="warning" style="color: red;">Red</div>
</html>
<style>
.warning {
color: red;
}
</style>
以下是两个包,它们可以满足OP对的要求
https://www.npmjs.com/package/juice
https://www.npmjs.com/package/inline-css