jquery context selector vs .find()

2019-06-19 08:12发布

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条回答
虎瘦雄心在
2楼-- · 2019-06-19 08:54

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.

查看更多
登录 后发表回答