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

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

问题描述

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

  $(< DIV>< / DIV>中); 

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



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

解决方案

我使用 $ .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天全站免登陆