javascript - 使用 JS 实现 HTML-minifier 插件

标签 javascript jquery html plugins minify

我正在寻找一种方法来缩小使用 Javascript 检索的 HTML 代码,如下所示

document.getElementById("content").innerHTML;

HTML 非常困惑,缩进也很糟糕,但它经过了 w3c 验证。我遇到了这个插件https://github.com/kangax/html-minifier但需要使用原始 JS/jQuery 实现的帮助,因为它只提供 Node.js 的说明,而且我的应用程序不是基于服务器的。

最佳答案

包含来自 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>

关于javascript - 使用 JS 实现 HTML-minifier 插件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27462674/

相关文章:

javascript - 将数组js变量传递给php

javascript - jQuery Masonry 未正确对齐或填充网格中的图像

javascript - 使用 Jquery 选择器验证是否已选择任何项目

javascript - jQuery - 一些数字的表计算出现奇怪的舍入错误

html - 调整窗口大小时如何使图像和文本在 HTML 中对齐?

jquery - 如何从文本区域中提取 DOM 对象?

javascript - OnClick 图像返回原始来源?

javascript - 如何解决 DOM hierarchy,Uncaught DOMException : Failed to execute '$x'

javascript - 在javascript中迭代多个数组

javascript - 包装组件意味着它失去了对重新渲染的关注