我师父(http://www.cnblogs.com/aaronjs/)说应当阅读框架(jquery),所以老夫就准备开始看了
然后公司的师兄原来写了个dom选择器,感觉不错啊!!!原来自己从来没有想过能写这些,所以我们今天一起来试试吧
我写的这个很简单,就是最简单的那种了,但是公司师兄写的很好。。。。。。
2 <div class="child1" id="c">
5 <input type="button" value="测试" class="child1.1" />
11 <div class="child2 child1">
12 <div class="child1.1">
13 <input class="l" value="三层测试" />
16 <input type="button" value="测试2" />
5 arr.push(document.getElementById(id))
8 '.': function (className) {
9 var els = document.getElementsByTagName('*');
10 var reg = new RegExp('(^|\\s)' + className + '(\\s|$)');
12 for (var i = 0, len = els.length; i < len; i++) {
13 if (reg.test(els[i].className)) {
19 'tag': function (tag) {
20 return document.getElementsByTagName(tag);
25 '#': function (el, id) {
26 return this.commomFunc(el, function (p) {
32 '.': function (el, className) {
33 var reg = new RegExp('(^|\\s)' + className + '(\\s|$)');
34 return this.commomFunc(el, function (p) {
35 if (reg.test(p.className)) {
40 'tag': function (el, tag) {
41 return this.commomFunc(el, function (p) {
42 if (p.tagName.toLowerCase() == tag) {
47 commomFunc: function (el, callback) {
51 p = p.parentNode || p.parentElement || null;
53 var b = callback && callback(p);
55 if (p.tagName == 'BODY') {
63 var getKV = function (str) {
64 if (!str || typeof str != 'string') return null;
65 var k = str.substring(0, 1);
67 if (k == '.' || k == '#') {
68 v = str.substring(1, str.length);
78 var query = function (str) {
80 return (queryFunc[kv.k] && queryFunc[kv.k](kv.v));
82 var filter = function (el, str) {
84 return (filterFunc[kv.k] && filterFunc[kv.k](el, kv.v));
86 var explodeSelector = function (str) {
89 str = str.replace(/\s+/g, ' ');
90 var arr = str.split(' ');
95 var queryAll = function (str) {
96 var arrSelector = explodeSelector(str);
97 var len = arrSelector.length;
102 var curSelector = null;
105 var els = query(arrSelector[len - 1]);
107 if (len == 1) return els;
109 while (index != -1) {
111 curSelector = arrSelector[index];
113 for (var i = 0, len = els.length; i < len; i++) {
115 if (filter(tmpEl, curSelector))
123 window.queryAll = queryAll;
var explodeSelector = function (str) {
str = str.replace(/\s+/g, ' ');
var arr = str.split(' ');
4 arr.push(document.getElementById(id))
7 '.': function (className) {
8 var els = document.getElementsByTagName('*');
9 var reg = new RegExp('(^|\\s)' + className + '(\\s|$)');
11 for (var i = 0, len = els.length; i < len; i++) {
12 if (reg.test(els[i].className)) {
18 'tag': function (tag) {
19 return document.getElementsByTagName(tag);
③ 根据选择器与获得的dom数组,判断其父元素是否具有相关属性(id,className,tag),有便留下来,没有就不管他
2 '#': function (el, id) {
3 return this.commomFunc(el, function (p) {
9 '.': function (el, className) {
10 var reg = new RegExp('(^|\\s)' + className + '(\\s|$)');
11 return this.commomFunc(el, function (p) {
12 if (reg.test(p.className)) {
17 'tag': function (el, tag) {
18 return this.commomFunc(el, function (p) {
19 if (p.tagName.toLowerCase() == tag) {
24 commomFunc: function (el, callback) {
28 p = p.parentNode || p.parentElement || null;
30 var b = callback && callback(p);
32 if (p.tagName == 'BODY') {
1 var queryAll = function (str) {
2 var arrSelector = explodeSelector(str);
3 var len = arrSelector.length;
8 var curSelector = null;
11 var els = query(arrSelector[len - 1]);
13 if (len == 1) return els;
17 curSelector = arrSelector[index];
19 for (var i = 0, len = els.length; i < len; i++) {
21 if (filter(tmpEl, curSelector))
尼玛已证明这是一个js的bug,我可以这么说么???
parseInt(33.1 * 3 * 100) / 100
本文转自叶小钗博客园博客,原文链接http://www.cnblogs.com/yexiaochai/p/3258279.html,如需转载请自行联系原作者