var sortOrder; if (document.documentElement.compareDocumentPosition) { sortOrder = function(a, b) { var ret = a.compareDocumentPosition(b) & 4 ? - 1: a === b ? 0: 1; if (ret === 0) { hasDuplicate = true; } return ret; }; } else if ("sourceIndex" in document.documentElement) { sortOrder = function(a, b) { var ret = a.sourceIndex - b.sourceIndex; if (ret === 0) { hasDuplicate = true; } return ret; }; } else if (document.createRange) { sortOrder = function(a, b) { var aRange = a.ownerDocument.createRange(), bRange = b.ownerDocument.createRange(); aRange.selectNode(a); aRange.collapse(true); bRange.selectNode(b); bRange.collapse(true); var ret = aRange.compareBoundaryPoints(Range.START_TO_END, bRange); if (ret === 0) { hasDuplicate = true; } return ret; }; }
上面这段代码是复制自jquery选择器Sizzle的,其中用到了几个相对生僻点的东西.
3个if子句。
compareDocumentPosition是用来比较两个元素之间关系的,dom level 3里边定义的。(参考地址:http://www.cnblogs.com/liuxinke/articles/2096010.html)
sourceIndex这个是ie特有的,用来标记元素在页面文档中的位置的属性.
compareBoundaryPoints是相对复杂点的东西,(参考地址:http://www.zhangxinxu.com/wordpress/?p=1591)
为了实现dom元素排序。