简体   繁体   English

使用 JavaScript 整理 HTML 输出

[英]Tidy HTML output with JavaScript

I have a large chunk of HTML.我有一大块 HTML。 In order for it to fit a certain container, I crop the HTML (not just the text) at, let's say, 200 characters.为了让它适合某个容器,我将 HTML(不仅仅是文本)裁剪为 200 个字符。 Obviously, some of the tags will remain unclosed in this case.显然,在这种情况下,某些标签将保持未关闭状态。 Is there a way, except writing the cleaner myself, to clean such cropped snippet without the server being involved?除了自己编写清理程序之外,有没有办法在不涉及服务器的情况下清理这样的裁剪片段?

Online services with public APIs that I can use from JavaScript are acceptable.我可以从 JavaScript 使用具有公共 API 的在线服务是可以接受的。

You can try the cutter.js library.您可以尝试cutr.js库。 It's pretty new, so I haven't heard much about it, but it seems like what you're looking for as far as cropping goes.它很新,所以我没有听说过太多关于它的信息,但是就裁剪而言,它似乎是您正在寻找的东西。

Check out my fiddle, testing it out: http://jsfiddle.net/JKirchartz/jwL8v/看看我的小提琴,测试一下: http : //jsfiddle.net/JKirchartz/jwL8v/

var oElement = document.getElementById("test");
Cutter.run(oElement, oElement, 100);

$("#gc").click(function(){
    /* This will count words by spaces in the text */
    var tt = $("#test").text().split(" ");
    if (typeof(console) == 'object'){    
        console.log(tt);
    }
    alert("wordcount: "+tt.length);
     
});

The Google Closure library has an HTML Pretty Printer module. Google Closure 库有一个 HTML Pretty Printer 模块。 You should be able to fork off it:您应该能够将其分叉:

Closure Library关闭库

Also, if you are using jQuery, try http://www.davidpirek.com/blog/html-beautifier-jquery-plugin .另外,如果您使用 jQuery,请尝试http://www.davidpirek.com/blog/html-beautifier-jquery-plugin

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM