使用 JavaScript 整洁的 HTML 输出

发布于 2025-01-07 01:36:55 字数 162 浏览 1 评论 0原文

我有一大块 HTML。为了使其适合特定容器,我将 HTML(不仅仅是文本)裁剪为 200 个字符。显然,在这种情况下,某些标签将保持未关闭状态。除了自己编写清理器之外,有没有办法在不涉及服务器的情况下清理此类裁剪后的代码片段?

我可以通过 JavaScript 使用带有公共 API 的在线服务。

I have a large chunk of HTML. In order for it to fit a certain container, I crop the HTML (not just the text) at, let’s say, 200 characters. 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

往日情怀 2025-01-14 01:36:55

您可以尝试 cutter.js 库。它很新,所以我没有听说过太多,但就裁剪而言,它似乎是您正在寻找的东西。

看看我的小提琴,测试一下: 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);
     
});

You can try the cutter.js library. 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/

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);
     
});
聆听风音 2025-01-14 01:36:55

Google Closure 库有一个 HTML Pretty Printer 模块。您应该能够分叉它:

Closure Library

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

The Google Closure library has an HTML Pretty Printer module. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文