Last active
December 3, 2015 03:53
-
-
Save leizongmin/7aa85d5ed8b435e8d873 to your computer and use it in GitHub Desktop.
简单选择器
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function (global, fullName, sortName) { | |
function Selector(q) { | |
if (q instanceof Selector) return q; | |
if (!(this instanceof Selector)) return new Selector(q); | |
var list; | |
if (typeof q === 'string') { | |
list = document.querySelectorAll(q); | |
Selector.from(list, this); | |
} else if (q instanceof Element) { | |
Selector.from([q], this); | |
} else if (q instanceof NodeList) { | |
Selector.from(q, this); | |
} else { | |
list = []; | |
} | |
}; | |
Selector.prototype.__proto__ = Array.prototype; | |
Selector.from = function (list, selector) { | |
if (!(selector instanceof Selector)) selector = new Selector(); | |
for (var i = 0; i < list.length; i++) { | |
selector[i] = list[i]; | |
} | |
selector.length = i; | |
return selector; | |
}; | |
Selector.extend = function (name, fn) { | |
Selector.prototype[name] = fn; | |
}; | |
Selector.extend('text', function (text) { | |
if (typeof text === 'undefined') { | |
return this.map(function (el) { | |
return el.innerText; | |
}).join(''); | |
} else { | |
this.forEach(function (el) { | |
el.innerText = text; | |
}); | |
return this; | |
} | |
}); | |
Selector.extend('html', function (html) { | |
if (typeof html === 'undefined') { | |
return this.map(function (el) { | |
return el.innerHTML; | |
}).join(''); | |
} else { | |
this.forEach(function (el) { | |
el.innerHTML = html; | |
}); | |
return this; | |
} | |
}); | |
Selector.extend('eq', function (i) { | |
return Selector.from(this.slice(i, i + 1)); | |
}); | |
Selector.extend('lt', function (i) { | |
return Selector.from(this.slice(0, i)); | |
}); | |
Selector.extend('gt', function (i) { | |
return Selector.from(this.slice(i + 1)); | |
}); | |
Selector.extend('each', function (fn) { | |
this.forEach(function (item) { | |
fn.call(item); | |
}); | |
}); | |
Selector.extend('remove', function () { | |
this.forEach(function (el) { | |
el.remove(); | |
}); | |
return this; | |
}); | |
Selector.extend('prop', function (name, value) { | |
if (this.length < 1) return; | |
if (typeof value === 'undefined') { | |
return this[0][name]; | |
} else { | |
this.forEach(function (el) { | |
el[name] = value; | |
}); | |
return this; | |
} | |
}); | |
Selector.extend('_setStyle', function (name, value) { | |
this.forEach(function (el) { | |
el.style[name] = value; | |
}); | |
return this; | |
}); | |
Selector.extend('css', function (name, value) { | |
if (typeof value === 'undefined') { | |
if (this.length < 1) return; | |
return this[0].style[name]; | |
} else { | |
return this._setStyle(name, value); | |
} | |
}); | |
Selector.extend('show', function () { | |
return this._setStyle('display', ''); | |
}); | |
Selector.extend('hide', function () { | |
return this._setStyle('display', 'none'); | |
}); | |
global[fullName] = Selector; | |
global[sortName] = Selector; | |
})(window, 'lightSelector', '$s'); | |
console.log($s('a').css('color')); |
Author
leizongmin
commented
Dec 3, 2015
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment