2013-08-03 136 views
1

有沒有辦法將html文檔中的硬編碼樣式提取到外部CSS文件?如果不是,你有一個想法如何做到這一點?你以前做過嗎?從HTML文件提取CSS樣式到外部CSS文件

自例如

<div style="background-color: red"> 
<a style="font-weight: bold"></a> 
</div> 

<div id='st-01'> 
<a id='st-02'><a/> 
</div> 

#st-01 { background-color: red } 
#st-02 { font-weight: bold } 

回答

0

你可以使用一些JS/jQuery代碼提取樣式,清除它們,給元素的ID,並添加了CSS。 檢查這個例子,你可以進一步擴展它。

$(document).ready(function(){ 
    var i = 0; 
    var css = ""; 
    $("div,a").each(function(){ 
     $(this).attr("id","st-"+i); 
     css += "#"+$(this).attr("id")+"{"+$(this).attr("style")+"}"; 
     $(this).removeAttr("style"); 
     i++; 
    }); 
    $("style").html(css); 
}); 

http://jsfiddle.net/d8TaJ/