2014-12-13 34 views
1

我正在尋找一種方式來縮小我的HTML代碼是用JavaScript檢索這樣實現HTML-minifier插件與JS

document.getElementById("content").innerHTML; 

的HTML是非常雜亂無章,縮進不佳,但它的W3C驗證。我遇到了這個插件https://github.com/kangax/html-minifier但是需要幫助實現原始JS/jQuery,因爲它只提供node.js的指令,而我的應用程序不是基於服務器的。

回答

1

將文件從dist directory包含在您的頁面中並使用minify函數。參考see the optionsdemo they provide

下面我實現它變成一個快速演示,消除屬性值引號:

$("#input").on("input", function() { 
 
    $("#output").val(minify($(this).val(), { 
 
    removeAttributeQuotes: true, 
 
    collapseWhitespace: true 
 
     // other options 
 
    })); 
 
});
<script src="https://rawgit.com/kangax/html-minifier/50fad6e3a7a77f37671afae6782f557bd686bc8b/dist/htmlminifier.min.js"></script> 
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<h1>Input</h1> 
 
<textarea id="input"></textarea> 
 

 
<h1>Output</h1> 
 
<textarea id="output"></textarea>

+0

您提供的演示實際上沒有縮小文件的代碼。 – guub 2014-12-13 19:40:57

+0

@Shabbb假設您定義*縮小*摺疊空格,請添加'collapseWhitespace:true'選項。請參閱編輯。 – 2014-12-13 19:45:40