jQuery:单独添加还是一次追加+选择器? [英] jQuery: append individually or at once + selectors?

查看:67
本文介绍了jQuery:单独添加还是一次追加+选择器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个jQuery UI小部件,当我初始化时,我创建了几个div,锚点,输入。我已经读过,构建整个HTML字符串然后将其附加到DOM比附加每个DOM元素更快。

I'm building a jQuery UI widget and when I initialize I create a several divs, anchors, inputs. I've read that it's faster to build up an entire HTML string and then append it to the DOM than to append each individual DOM element.

但是,我需要访问那些元素以后,所以我也倾向于单独构建它们然后追加它们所以我已经将它们缓存而不是在我追加所有HTML之后选择它们。

However, I need access to those elements later, so I'm also inclined to build them individually and then append them so I already have them cached rather than selecting them after I append all the HTML.

什么通常更快?

例如:

var html = $('<div id="container">.....several more divs, anchors and inputs all with IDs......</div>');

<dom element>.append(html);
this.container = $('#container');
this.search = $('#search');
...and so on

this.container = $('<div id="container"></div>');
this.search = $('<input .... id="search" />');
...and the rest of the divs, anchors and inputs then...

<dom element>.append(this.container).append(this.search) ..... and so on


推荐答案

我也建议阅读这些两个博客帖子

I also suggest reading these two blog-posts

43,439正确使用append()的理由

关于如何使用jQuery中的大数据集提高代码性能的5个简单提示

这篇关于jQuery:单独添加还是一次追加+选择器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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