2013-04-26 119 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