使用jQuery创建HTML元素最有效的方法是什么?(What is the most efficient way to create HTML elements using jQuery?)

编程入门 行业动态 更新时间:2024-10-28 03:28:17
使用jQuery创建HTML元素最有效的方法是什么?(What is the most efficient way to create HTML elements using jQuery?)

最近我一直在做很多模态窗口弹出窗口,而不是,我使用了jQuery。 我用来在页面上创建新元素的方法绝对是:

$("<div></div>");

但是,我觉得这不是最好的或最有效的方法。 从性能角度来看,在jQuery中创建元素的最佳方式是什么?

这个答案有以下建议的基准。

Recently I've been doing a lot of modal window pop-ups and what not, for which I used jQuery. The method that I used to create the new elements on the page has overwhelmingly been along the lines of:

$("<div></div>");

However, I'm getting the feeling that this isn't the best or the most efficient method of doing this. What is the best way to create elements in jQuery from a performance perspective?

This answer has the benchmarks to the suggestions below.

最满意答案

我使用$(document.createElement('div')); 基准测试表明这种技术是最快的。 我推测这是因为jQuery不必将其标识为元素并创建元素本身。

您应该使用不同的JavaScript引擎来运行基准测试,并根据结果衡量您的受众。 从那里作出决定。

I use $(document.createElement('div')); Benchmarking shows this technique is the fastest. I speculate this is because jQuery doesn't have to identify it as an element and create the element itself.

You should really run benchmarks with different Javascript engines and weigh your audience with the results. Make a decision from there.

更多推荐

本文发布于:2023-07-20 12:34:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1199612.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:最有效   元素   方法   HTML   jQuery

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!