1
的任务是在jQuery的风格编写简单的框架,来解决这个我已经写代码:简单的js框架,DOM。 (模块式的,如jQuery)问题与添加方法
(function(window) {
window.smp=function smpSelector(selector) {
return new smpObj(selector);
}
function smpObj(selector) {
this.length = 0;
if (!selector) {
this.el = [];
return this;
}
if(typeof selector == 'string'){
if(selector[0]==='#'){
this.el = [document.getElementById(selector.slice(1, selector.length))];
this.length = 1;
return this;
} else if(selector[0]==='.'){
this.el = document.getElementsByClassName(selector.slice(1, selector.length));
this.length = this.el.length;
return this;
} else {
this.el = document.getElementsByTagName(selector);
this.length = this.el.length;
return this;
}
}
else return null;
}
window.smp.changeColor=function smpColor(color) {
for (var i = 0; i < this.length; i++)
this.el[i].style.color = color;
}
})(window);
,它的工作确定。我可以打电话给这样的:
smp('div')
但后来我试着添加方法:
window.smp.changeColor=function smpColor(color) {
for (var i = 0; i < this.length; i++)
this.el[i].style.color = color;
}
而且它不能正常工作。
smp('div').changeColor('color')
(不能这样调用)
我会很感激,如果有人能告诉我的错误。
我用这篇文章article
哦,我'坏。 非常感谢,您的回答解决了问题。 非常感谢您的帮助! – bordus
很高兴帮助:)。顺便说一句,你能将我的答案标记为正确答案吗? :) – BnoL
想想我还有一个问题。 这是一个有点离题,但MB你会回答。 因为我'不是JS-dev的我不知道,它是确定在我changeColor FUNC使用循环?还是更灵活的方式存在? – bordus