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

查看:31
本文介绍了使用 jQuery 创建 HTML 元素的最有效方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我一直在做很多模态窗口弹出窗口,我使用了jQuery.我用来在页面上创建新元素的方法绝大多数是这样的:

$("

");

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

此答案具有以下建议的基准.

解决方案

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

您真的应该使用不同的 Javascript 引擎运行基准测试,并根据结果权衡您的受众.从那里做出决定.

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.

解决方案

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.

这篇关于使用 jQuery 创建 HTML 元素的最有效方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆