$('
', {id: 'bla'}) 与 $('
;);

发布于 2024-11-27 22:28:54 字数 297 浏览 5 评论 0原文

之间的速度有什么区别吗

$('<div>', {id: 'bla', click: func, css: { opacity:0.5 } }

和全部内联执行

$('<div id="bla" style="opacity:0.5"></div>').click(func);

?对于第二个示例,jquery 在内部做了什么?只需调用 .innerHTML 还是尝试解析它,然后执行与第一个示例完全相同的操作?

Is there any difference in speed between:

$('<div>', {id: 'bla', click: func, css: { opacity:0.5 } }

and doing it all inline?

$('<div id="bla" style="opacity:0.5"></div>').click(func);

What does jquery do internally for the second example? Just call .innerHTML or does it try to parse it and then do it exactly the same was as the first example?

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

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

发布评论

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

评论(2

ぺ禁宫浮华殁 2024-12-04 22:28:54

当我们将 html 标记作为输入传递给 $() 时,它使用 document.createDocumentFragment 动态创建元素,然后使用 childnodes 属性检索实际元素并执行所需的操作。

When we pass html markup as an input to $() it uses document.createDocumentFragment to create the elements on the fly and then uses childnodes property to retrieve the actual elements and performs the required operation.

雨落□心尘 2024-12-04 22:28:54

确定一个片段是否优于另一个片段的最佳方法是自己对它们进行基准测试。

您可以使用 jsPerf.com 对您的代码进行基准测试。

事实证明,第二个版本在 Firefox 中速度更快,并且在 Chrome 上几乎相同。

http://jsperf.com/test-jquery-element-creator

The best way to decide if one snippet is better than the other is to benchmark them yourself.

You can use jsPerf.com to benchmark your code.

Turns out the second version is faster in Firefox and they are about the same on Chrome.

http://jsperf.com/test-jquery-element-creator

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