如何以最少的延迟将省略号插入到 HTML 中的大型数据集中

发布于 2024-10-31 20:42:02 字数 714 浏览 1 评论 0原文

如果内容太宽,则将省略号 (...) 插入 HTML 标记 提到如何将省略号插入 HTML 内容。这个解决方案有效,但我发现当它应用于大型数据集(想象 1,000 个 div)时,它往往会变得相当慢。

我的 JSFiddle 演示了这个问题。将我设置的变量 (numberOfDivs) 从 10 更改为 100 会显着增加输出结果所需的时间。将此数字从 100 更改为 1000,会导致 Chrome 想要终止该页面。

看起来 Jquery 的解决方案以及我见过的其他解决方案都涉及用文本填充 DOM 中的元素(我相信我使用的 Jquery 插件做了类似的事情,对于大型数据集来说似乎很昂贵)或使用功能IE7 和其他旧版浏览器不支持 (text-wrap:ellipsis)。

在处理大量数据时,对 get element.offsetWidthelement.clientWidth 的任何调用似乎都会变得昂贵。

有没有人找到任何优化或强大的服务器端解决方案?当我说强大的服务器端解决方案时,我的意思是考虑到文本的大小和包含的 div(或其他元素)的大小。简单地执行文本的子字符串是一种解决方案,但我认为这并不可靠。

Insert ellipsis (...) into HTML tag if content too wide mentions how to insert ellipsis into HTML content. This solution works, but I find that when it is applied to a large dataset (imagine 1,000 div's) it tends to get pretty slow.

My JSFiddle demonstrates this issue. Changing the variable I set (numberOfDivs) from 10 to 100 dramatically increases the time it takes to output the results. Changing this number from 100 to 1000, causes Chrome to want to kill the page.

It seems like Jquery's solution, and the others I have seen, all involve either populating an element in the DOM with the text (I believe the Jquery plugin I used does something of the sort, it seems expensive for large datasets) or using a feature not supported by IE7 and other older browsers (text-wrap:ellipsis).

It also seems like any calls to get element.offsetWidth or element.clientWidth get expensive when dealing with large sets of data.

Has anyone found any optimizations or robust server-side solutions? When I say robust server-side solution, I mean one that takes into account the text's size and size of the containing div (or other element). Simply doing a substring of the text is one solution, but I would not consider that robust.

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

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

发布评论

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

评论(2

靑春怀旧 2024-11-07 20:42:02

没有人会找到一个强大且可靠的解决方案。对此的高性能解决方案,涵盖所有浏览器,并且在所有浏览器中同样有效,因为它不存在。唯一强大的&高性能的解决方案是纯 CSS,它仅受大多数人希望支持的浏览器子集的支持。

在您希望支持的所有浏览器都支持 text-overflow: ellipsis 之前,您可以拥有健壮的(查看所有参数的慢速 js)或高性能的(无论是服务器端还是客户端的子字符串)。

唯一不支持 css 技巧的浏览器是 firefox。
IE7+以及chrome、safari等webkit浏览器都支持得很好。
Opera 通过 -o-text-overflow 支持它。

如果您决定不支持 Firefox,您可以通过以下方式支持所有其他浏览器:

white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
-o-text-overflow: ellipsis;

您需要 white-space: nowrap; > 和 overflow: hide; 以使 text-overflow 正常工作。

如果您可以忍受文本不可选择的情况,那么您可以使用 XUL hack for firefox,但我自己很少发现它有用。

Nobody will have found a robust & performant solution for this, that covers all browsers and works equally well in all of them since it doesn't exist. The only robust & performant solution would be pure css, which is only supported by a subset of the browsers most people wish to support.

Until all browsers you wish to support, support text-overflow: ellipsis, you can have either robust (slow js that looks at all parameters) or performant (substring whether server or clientside).

The only browser that doesn't support the css trick is firefox.
IE7+,and webkit browsers like chrome and safari supports it just fine.
Opera supports it by -o-text-overflow

Should you decide to not support firefox, you can support all the other browsers by doing this:

white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
-o-text-overflow: ellipsis;

You need white-space: nowrap; and overflow: hidden; to make the text-overflow work well.

If you can live with text not being selectable then you can use the XUL hack for firefox, but i rarely find it usefull myself.

时光病人 2024-11-07 20:42:02

我过去已经解决过类似的问题,并且以下优化对于我们的目的来说效果更好。它的性能更好,特别是对于比所需空间长得多的文本。但是 1000 div 的...不知道...

而不是提到的 while 循环 解决方案,尝试如下操作:

var diff = t.width() - el.width();
if (diff <= 0)
    return;
else {
    var avgWidth = t.width() / text.length;
    var redundantChars = diff / avgWidth;
    text = text.substr(0, text.length - redundantChars);
    t.html(text + "...");
}

while (text.length > 0 && t.width() > el.width()) {
    text = text.substr(0, text.length - 1);
    t.html(text + "...");
}

while (text.length < originalText.length && (t.width() + avgWidth) < el.width()) {
    text = originalText.substr(0, text.length + 1);
    t.html(text + "...");
}

I've solved similar issue in the past and following optimization performed better for our purpose. It performs better especially for texts much longer than required space. But 1000 div's... don't know...

Instead of the while loop in the mentioned solution, try something like this:

var diff = t.width() - el.width();
if (diff <= 0)
    return;
else {
    var avgWidth = t.width() / text.length;
    var redundantChars = diff / avgWidth;
    text = text.substr(0, text.length - redundantChars);
    t.html(text + "...");
}

while (text.length > 0 && t.width() > el.width()) {
    text = text.substr(0, text.length - 1);
    t.html(text + "...");
}

while (text.length < originalText.length && (t.width() + avgWidth) < el.width()) {
    text = originalText.substr(0, text.length + 1);
    t.html(text + "...");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文