jquery context selector vs .find()

一曲冷凌霜 提交于 2019-12-23 09:00:09

问题


What is more efficient?

var container = $("#container");

// 1
var links1 = container.find("a");

// 2
var links2 = $("a", container);

I personally prefer $("a", container) because it looks better, but are they different in performance?


回答1:


The context selector $("a", container) is converted to find. find() will be faster but in most cases this could be ignored. I would go for find() as its syntax is quite stright forward for me. This post has performance comparison that would help you deciding which one you would use.



来源:https://stackoverflow.com/questions/18225727/jquery-context-selector-vs-find

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!