$('', {id: 'bla'}) 与 $(';);
之间的速度有什么区别吗
$('<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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当我们将 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.
确定一个片段是否优于另一个片段的最佳方法是自己对它们进行基准测试。
您可以使用 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