内联css提取
Inline css extraction
我想提取javascript或jquery中应用于文本"Hello World"的样式。
例如,假设这是在"Hello World"上应用样式的html
<div id="root" style="text-align: right;">
<span style="font-weight: bold; text-decoration: underline;">Hello World</span>
</div>
我想通过javascript或jquery中更智能的方式提取"Hello World"上应用的所有样式。输出应为text-align:right;font weight:bold;文本装饰:下划线
我该怎么做?请告诉任何人。
问候
-------------更新------------------
感谢大家的宝贵意见。但我仍然没有得到解决方案。我再次想用代码来进一步澄清问题陈述。
静态html:
<div id="contentplaceholder">
</div>
拖放小部件后,会生成动态html。
动态生成的html:
<div id="contentplaceholder">
<div style="height: 0px; width: 0px; position: absolute; z-index: 0; left: 0pt; top: 0pt;" title="textWidget" id="contTextarea1329455914127">
<div style="border: 1px dotted rgb(102, 102, 102); height: 150px; width: 300px; float: left; overflow: hidden; left: 561.033px; top: 166.5px; position: relative; display: block;" id="Textarea1329455914127" class="ui-draggable ui-resizable">
<div class="ui-resizable-handle ui-resizable-e"></div>
<div class="ui-resizable-handle ui-resizable-s"></div>
<div class="ui-resizable-handle ui-resizable-se ui-icon ui-icon-gripsmall-diagonal-se" style="z-index: 1001;"></div>
</div>
</div>
现在,我们可以在下拉区域中编写任何类似Hello World的文本,并可以使用提供的小部件工具栏应用样式。
现在最后的html是:
<div id="contentplaceholder">
<div style="height: 0px; width: 0px; position: absolute; z-index: 0; left: 0pt; top: 0pt;" title="textWidget" id="contTextarea1329455914127">
<div style="border: 1px dotted rgb(102, 102, 102); height: 150px; width: 300px; float: left; overflow: hidden; left: 561.033px; top: 166.5px; position: relative; display: block;" id="Textarea1329455914127" class="ui-draggable ui-resizable">
<!-- Dynamically applied inline css on Hello World and need to extract style only for this area-->
<!-- Start -->
<div style="text-align: right;">
<span style="font-weight: bold; text-decoration: underline;">HELLO WORLD</span>
<br>
</div>
<!-- End -->
<div class="ui-resizable-handle ui-resizable-e"></div>
<div class="ui-resizable-handle ui-resizable-s"></div>
<div class="ui-resizable-handle ui-resizable-se ui-icon ui-icon-gripsmall-diagonal-se" style="z-index: 1001;"></div>
</div>
</div>
现在,我只想提取应用于"你好世界"的风格。这里只有根div是固定的,所有带id的子div都是动态生成的,而不是固定的,唯一的标题是我得到了相同的标题,比如title="textWidget"
请提供帮助来完成这项任务。
问候
退房http://forum.jquery.com/topic/css-with-no-arguments-should-return-a-object-of-style-attributes
我已经用过几次了,并完成了这项工作。。。。将从CSS文件和Inline:中获得样式
function css(a) {
var sheets = document.styleSheets,
o = {};
for (var i in sheets) {
var rules = sheets[i].rules || sheets[i].cssRules;
for (var r in rules) {
if (a.is(rules[r].selectorText)) {
o = $.extend(o, css2json(rules[r].style), css2json(a.attr('style')));
}
}
}
return o;
}
function css2json(css) {
var s = {};
if (!css) return s;
if (css instanceof CSSStyleDeclaration) {
for (var i in css) {
if ((css[i]).toLowerCase) {
s[(css[i]).toLowerCase()] = (css[css[i]]);
}
}
} else if (typeof css == "string") {
css = css.split("; ");
for (var i in css) {
var l = css[i].split(": ");
s[l[0].toLowerCase()] = (l[1]);
};
}
return s;
}
用法:var style = css($("#element"));
$('#contentplaceholder span').first().attr('style')将完整返回样式文本。
我建议您标识跨度,并使用它来引用标记,只有当您只想获得style属性的文本内容时,才使用此方法。
- CSS-如何定位内容数据标题
- 窗口大小html css
- 如果文本字段为空,则使用JavaScript应用CSS样式
- 动画.CSS重播
- 有没有一种方法可以防止img get请求使用css或js发生
- JavaScript无法提取引号css属性
- Galleria致命错误:无法从CSS/Multiple Galleria和Display中提取舞台高度:none;
- 提取concernet html的所有内联css
- 如何使用正则表达式从字符串中提取CSS规则
- 将html文档中的css样式提取到外部css文件中
- 内联css提取
- 如何在dom-node上获取和提取匹配的css规则
- 从HTML文本中提取所有CSS类
- 提取字段值并将其应用为CSS颜色值
- 如何解析和提取CSS选择器作为字符串
- 从JavaScript的css规则中提取RGB和RGBA
- 提取所有CSS类和ID's在给定的HTML块中
- 将HTML中的CSS提取/解码为Python
- 如何从 CSS 选择器中提取类名
- 使用 getElementById 提取 CSS 数据