| 581 | |
| 582 | |
| 583 | function init(selector, context) { |
| 584 | |
| 585 | if (selector instanceof HTMLElement) { // 单个 html element 节点 |
| 586 | [].push.call(this, selector); |
| 587 | return this; |
| 588 | }; |
| 589 | |
| 590 | if (Array.isArray(selector)) {// 单纯的元素组成的数组 |
| 591 | // 去重 |
| 592 | var unique = []; |
| 593 | selector.forEach(function (node) { |
| 594 | if (node.nodeType === 1 && unique.indexOf(node) == -1) { |
| 595 | unique.push(node); |
| 596 | }; |
| 597 | }); |
| 598 | |
| 599 | [].push.apply(this, unique); |
| 600 | return this; |
| 601 | }; |
| 602 | |
| 603 | if (typeof selector == 'string') {// css字符串 |
| 604 | if (!context || !context.nodeType || context.nodeType != 1) {// 不是元素节点的话,context指向document |
| 605 | context = document; |
| 606 | }; |
| 607 | selector = context.querySelectorAll(selector); // NodeList |
| 608 | |
| 609 | [].push.apply(this, selector); |
| 610 | return this; |
| 611 | }; |
| 612 | |
| 613 | if (selector instanceof NodeList) {// NodeList |
| 614 | // 去除非元素节点 |
| 615 | var elems = []; |
| 616 | each(selector, function () { |
| 617 | if (this.nodeType == 1) { |
| 618 | elems.push(this); |
| 619 | }; |
| 620 | }); |
| 621 | |
| 622 | [].push.apply(this, elems); |
| 623 | return this; |
| 624 | }; |
| 625 | |
| 626 | if (selector instanceof HTMLCollection) { // HTMLCollection |
| 627 | [].push.apply(this, selector); |
| 628 | return this; |
| 629 | }; |
| 630 | |
| 631 | }; |
| 632 | |
| 633 | // 遍历类数组 |
| 634 | function each(elems, fn) { |