所有 jQuery 选择器都返回一个对象数组。这些对象的顺序是否总是与它们在 HTML 中的顺序相同?我可以依靠它吗?
最佳答案
是的。
jQuery 1.3.2 release notes说:
Elements Returned in Document Order
This is a change to jQuery's selector engine that re-orders the returned results to be in document order, instead of the order in which the selectors were passed in. This change was done in order to be in compliance with the Selectors API specification (which jQuery uses, internally, in browsers that support it).
jQuery 1.3 上的情况并非如此:
The order of "a, b, c" style selectors may change. Browsers that support querySelectorAll (Safari, Firefox 3.5+, Opera 10+, IE 8+) will return the elements in document order, other browsers will (currently) return them in the order specified. In 1.3.2 and later release all comma-separated selectors will be returned in document order.
关于jquery - 订单对象是否由指定的 jQuery 选择器返回?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1636201/