2013-04-26 98 views
0

我必须做HTML表格,以Excel文件,我找到一个JavaScript代码,它的工作很好,但我的网站有元素的类。我希望这些类的属性与jQuery的功能样式属性或东西。CSS类风格

CSS

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

HTML

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

即是我想要;

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

当我这样做时,我的excel文件将被格式化,就像我的网站。有什么办法可以做到这一点?

+0

怎么样查找和替换? – 2013-04-26 11:24:29

+3

我认为你正在寻找这个东西。 http://stackoverflow.com/questions/4307409/copying-css-to-inline-using-jquery-or-retaining-formatting-when-copying-stuff-f – rahularyansharma 2013-04-26 11:26:41

+0

http://jsfiddle.net/rahularyansharma/E57Xu/你在找这个吗? – rahularyansharma 2013-04-26 11:28:36

回答

2

我在这里张贴这只是监守OP想在这里。我从另一个so answer which can be find here采取了此解决方案。

(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(); 

MY JS FIDDLE LINK can also be find here

0

使用jQuery CSS方法来应用

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

,或者您可以通过addClass()也添加类表

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

0

这里css选择器为所有表格添加样式:

table 
{ 
    background : black; color:white; 
} 
4

所以你应该重写全部样式(内联,外部等)与元素相关联的“样式”属性。

在下面的链接,有一个解决方案是如何做到这一点: Can jQuery get all CSS styles associated with an element?

+0

三个答案后,我认为你只有正确阅读问题的人。这是另一个答案,它与现在几乎相同http://stackoverflow.com/questions/4307409/copying-css-to-inline-using-jquery-or-retaining-formatting-when-copying-stuff-f谢谢 – rahularyansharma 2013-04-26 11:33:32