比 jQuery 的 .html 函数更快的替代方案?

发布于 2024-08-07 22:13:29 字数 194 浏览 2 评论 0原文

我想知道 jquery .html(variable) 函数是否有替代方法?它似乎相当慢,并且在将 html 插入 DOM 时也会冻结。

我尝试使用innerHtml,效果很好,速度也非常快。但由于某种原因,当我在插入的代码中有任何 jquery 或 MS ajax 时,这些脚本只能在 FF 中工作。

我正在尝试插入大约 2000 行代码。

I was wondering if there is an alternative to the jquery .html(variable) function? It seems to be quite slow and also freezes while inserting the html into the DOM.

I tried using innerHtml, and that works great, extremely fast as well. But for some reason, when I have any jquery or MS ajax in the code that is inserted, those scripts ONLY work in FF.

I'm trying to insert around 2000 lines of code.

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

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

发布评论

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

评论(3

花间憩 2024-08-14 22:13:29

问题是您在页面中插入的 jquery 或 ms ajax (javascript) 代码不会被评估。 jquery 中的 .html 方法可以为您完成此操作,但如果您想使用 .innerHtml,则必须使用 eval() 函数手动评估它:

eval(«javascript code here»);

因此步骤是,使用 innerHtml 插入 html,然后仅评估 javascript部分使用 eval 函数。

The problem is that the jquery or ms ajax (javascript) code you insert in the page is not evaluated. the .html method in jquery does it for you, but if you want to use .innerHtml, you have to evaluate it manually, using the eval() function:

eval(«javascript code here»);

So the steps are, insert the html using innerHtml, then evaluate only the javascript part using the eval function.

余厌 2024-08-14 22:13:29

如果您能更具体地说明您的问题,那就太好了。
一般来说,不需要进行 2000 次 .html() 更新,您必须将其加入到更大的 .html() 调用中,因为每次 .html() 都会重建整个 DOM,这会导致速度变慢。

Would be great, if you were more specify about your problem.
Generally speaking, there is no need to make 2000 .html() updates, you have to join it into bigger .html() calls, because after each .html() whole DOM is rebuild, which causes slow down.

凉城已无爱 2024-08-14 22:13:29

我刚刚阅读这个答案 并且评论中有一个关于使用 DOM 文档片段 及其速度的链接与“正常”方法相比......检查一下。它实际上并没有将基准测试与 jQuery 进行比较,但它会给您一个总体思路。

I was just reading this answer and there was a link in the comments about using DOM document fragments and how fast it is compared to the "normal" method ... check it out. It doesn't actually compare the benchmarks to jQuery, but it'll give you a general idea.

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