如何給css樣式加上父節點?

來源:互聯網
上載者:User

在網上找到一個灰常好看的html模版,很可惜它內建了很多css樣式,為了防止衝突,你需要一行一行的去改css樣式,給它添加上父親節點,並且改動css的選取器。工作量太大有木有啊。 這個代碼可以給css添加上父親節點。有效防止了css衝突。

注意2點:
1. css檔案中需要手動去掉注釋,該功能沒有寫了。有興趣的可以將代碼補全
2. 最好在每個選取器的 ”{“ 換行,該代碼沒有寫,有興趣可以自己補全
比如:

#div1{ color : red; }改成#div1{    color:red;}
package test.css;import java.io.BufferedReader;import java.io.File;import java.io.FileReader;import java.io.FileWriter;import org.junit.Test;/** * 給所有的css樣式加上父節點 *  * @author Van * @time 20150312 */public class ChangeCss {private static String tagName = "#footOuter ";private static String inPath = "D:\\workspaceluna\\GGBlog\\src\\main\\webapp\\resources\\plugins\\footer\\style.css";private static String outPath = "D:\\workspaceluna\\GGBlog\\src\\main\\webapp\\resources\\plugins\\footer\\style1.css";@Testpublic void cssTest() throws Exception{//輸入BufferedReader in = new BufferedReader(new FileReader(inPath));//刪除舊檔案File file = new File(outPath);file.delete();//建立輸出資料流FileWriter fw = new FileWriter(outPath);   int lineTotal = 0;String temp;while( (temp = in.readLine()) != null ){lineTotal++;StringBuilder sbder;//跳過空行if(isEmpty(temp)){continue;}//跳過@開頭的行if(temp.contains("@")){fw.write(temp+"\r\n");continue;}//對有逗號和沒有分號的行加標籤if(temp.contains(",") && !temp.contains(";")){sbder = new StringBuilder();String _temp[] = temp.split(",");for(String _eveString : _temp){//加上父節點sbder.append( tagName + _eveString);if(_eveString.contains("{") || _eveString.contains("}"))continue;sbder.append(",");}fw.write(sbder+"\r\n");continue;}//對有花括弧和逗號的行進行處理if(temp.contains("\\{")){if(temp.contains(",")){sbder = new StringBuilder();temp = temp.split("\\{")[0];String _temp[] = temp.split(",");for(int i=0;i<_temp.length;i++){//加上父節點sbder.append( tagName + _temp[i]);if(i==_temp.length-1) break;sbder.append(",");}sbder.append(temp.split("\\{")[1]);fw.write(sbder+"\r\n");continue;}}//對有花括弧的行進行處理if(temp.contains("{")){sbder = new StringBuilder();sbder.append(tagName + temp);fw.write(sbder+"\r\n");continue;}fw.write(temp+"\r\n");}System.out.println(lineTotal);in.close();fw.close();}private boolean isEmpty(String value){if(value.isEmpty()) return true;if(value.matches("^\\s+$")) return true;return false;}}
相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.