c# - 使用样式定义解析类属性

标签 c# html css regex styles

我会具体一点:

如何替换

<style type="text/css">
   .class1 {font-weight:bold; font-size:10pt;}
   .class2 {font-weight:bold; font-size:12pt;}
   ...
   .classN {font-weight:bold; font-size:8pt; vertical-align:sub;}
</style>

<div class="class2" style="color:blue;">
   Bold Text
</div>

有了这个:

<div style="color:blue; font-weight:bold; font-size:12pt;">
   Bold Text
</div>

**请注意 - 节点可以是任何节点 - 属性顺序无关紧要。 - 类属性不需要被剥离

有任何 HTML 方法 (C#) 可以做到这一点吗?正则表达式?有什么想法吗?

提前致谢!

最佳答案

如果你使用的是jquery,你可以使用这个插件:

/*
 * getStyleObject Plugin for jQuery JavaScript Library
 * From: http://upshots.org/?p=112
 *
 * Copyright: Unknown, see source link
 * Plugin version by Dakota Schneider (http://hackthetruth.org)
 */

(function($){
    $.fn.getStyleObject = function(){
        var dom = this.get(0);
        var style;
        var returns = {};
        if(window.getComputedStyle){
            var camelize = function(a,b){
                return b.toUpperCase();
            }
            style = window.getComputedStyle(dom, null);
            for(var i=0;i<style.length;i++){
                var prop = style[i];
                var camel = prop.replace(/\-([a-z])/g, camelize);
                var val = style.getPropertyValue(prop);
                returns[camel] = val;
            }
            return returns;
        }
        if(dom.currentStyle){
            style = dom.currentStyle;
            for(var prop in style){
                returns[prop] = style[prop];
            }
            return returns;
        }
        return this.css();
    }
})(jQuery);

然后尝试这样的事情

<div class="class2" style="color:blue;">
   Bold Text
</div>
<script type="text/javascript">
  var computedStyle;
  $("div[class^='class']").each(function(){
    computedStyle = $(this).getStyleObject();
    $(this).css(computedStyle);
    $(this).attr('class','');
  });
</script>

关于c# - 使用样式定义解析类属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10144550/

相关文章:

c# - 如何保护在 SignalR 中监听的事件?

c++ - 使用 Poco 库在邮件中发送 HTML 代码

javascript - 使用输入类型文本以表单形式上传图像

即使使用相同的 css 文件,CSS 页面也不相等

css - IE9 中的 HTML5 视频在其两侧显示黑色边框

c# - 检查时间(考勤 c#)

c# - 如何从 XML 中获取值?

c# - 如何将数据从 aspx 页面传递到 ascx 模式弹出窗口?

javascript - 是否可以在 Gulp 中忽略带有 unCSS 的文件?

javascript - 函数返回的正确语法以更改 Javascript 中的 div 尺寸