0

我必须将 html 表转换为 excel 文件,我为它找到了一个 javascript 代码,它工作得很好,但我的网站有元素类。我希望使用 jquery 函数或其他东西将类属性设置为样式属性。

例子

css

.myTable
{
   background : black; color:white;
}

html

<table class="myTable"></table>

那就是我想要的;

<table style="background : black; color:white;"></table>

当我这样做时,我的 excel 文件将像在我的网站中一样被格式化。有什么办法吗?

4

4 回答 4

4

因此,您应该将与元素关联的所有样式(内联、外部等)重写为它的“样式”属性。

在以下链接下,有一个解决方案: Can jQuery get all CSS styles associated with an element?

于 2013-04-26T11:30:55.550 回答
2

我在这里发布这个只是因为 OP 想要它在这里。我从另一个解决方案中获取了这个解决方案,因此可以在此处找到答案

  (function($) {
        $.extend($.fn, {
            makeCssInline: function() {
                this.each(function(idx, el) {
                    var style = el.style;
                    var properties = [];
                    for(var property in style) { 
                        if($(this).css(property)) {
                            properties.push(property + ':' + $(this).css(property));
                        }
                    }
                    this.style.cssText = properties.join(';');
                    $(this).children().makeCssInline();
                });
            }
        });
    }(jQuery));


    $('.myTable').makeCssInline();

我的 JS FIDDLE 链接也可以在这里找到

于 2013-04-26T11:40:25.923 回答
0

使用jquery css方法申请

$('table').css({background:black,color:white}); //this will apply for all tables

或者您也可以通过 addClass() 将该类添加到表中

$('table').addClass('myTable');//这将适用于所有表

于 2013-04-26T11:24:32.330 回答
0

这是用于设置所有表格样式的 css 选择器:

table
{
   background : black; color:white;
}
于 2013-04-26T11:29:33.553 回答