简体   繁体   English

使用javascript将外部和内部样式转换为嵌入式样式

[英]Convert external and internal styles into inline styles with javascript

In order to make html content with styles more portable, I need to convert all external styles ( <link> importing external .css) and internal styles (defined with <style> s) into the inline styles of the elements. 为了使具有样式的html内容更具可移植性,我需要将所有外部样式( <link>导入外部.css)和内部样式(由<style>定义)转换为元素的内联样式。

For example, if I have: 例如,如果我有:

<link rel="stylesheet" href="common.css">
<style>
.alert { color: red; font-weight: bold; }
</style>

<div id="foo" class="special alert" style="font-size: 2em;">something</div>

and a common.css with: 和common.css与:

.special { border: 1px solid dotted; }

I want a result like: 我想要这样的结果:

<div id="foo" class="special alert" style="border: 1px solid dotted; color: red; font-weight: bold; font-size: 2em;">something</div>

Currently the problem is how to get all external and internal styles that are applied to the div[#foo]? 当前的问题是如何获取应用于div [#foo]的所有外部和内部样式?

How many CSS properties do you have? 您有几个CSS属性? if not so many, you can do something like $("#foo").css("border", "1px"); 如果不是很多,您可以执行类似$(“#foo”)。css(“ border”,“ 1px”);

if there are a lot of CSS files, its always not suggested do inline style 如果有很多CSS文件,则始终不建议使用内联样式

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

相关问题 如何使用客户端 javascript 将外部/内部 css 样式转换为内联样式属性 - How to convert external/internal css styles into inline style-attribute using client-side javascript 将外部CSS样式表转换为内联样式,或转换为<style> tag, via Javascript - Convert external CSS stylesheet into inline styles, or into a <style> tag, via Javascript 使用 javascript 将 css styles 转换为内联 styles - Convert css styles to inline styles with javascript keeping the style units 在动态html中呈现外部,内部,内联CSS样式之间的区别 - Difference between rendering external, internal, inline css styles in dynamic html Javascript 可以检测内联样式吗? - Can Javascript Detect Inline Styles? 如何删除外部脚本添加的内联样式? - How to remove inline styles added by external script? 可以将基于类的样式转换为 VueJS 组件的内联样式吗? - Possible to convert class based styles to inline styles for VueJS component? 使用JavaScript / jQuery删除/更改内部样式表样式 - Remove/change Internal styles sheet styles with JavaScript/jQuery 用于添加内联样式的 jQuery/JavaScript 切换按钮 - jQuery/JavaScript toggle button to add Inline styles 如何将嵌套的 javascript object 转换为 CSS ZBC4150D023D32593136DB671D6Z1 的内联字符串? - How can I convert a nested javascript object into an inline string of CSS styles?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM