これは単なる練習です。私はこのmaterial単純なjsフレームワーク、DOM。 (モジュールスタイル、jQueryスタイルのようにしようとする)
(function(window) {
function smp(selector) {
return new smpObj(selector);
}
function smpObj(selector) {
this.length = 0;
if (!selector) {
this.el = [];
return this;
}
if (selector.nodeType) {
this.el = selector;
this.length = 1;
return this;
} else 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.lenghth=this.el.length
return this;
}
else return null;
}
})(window);
を使用して、このようなスクリプトを書いているこの問題を解決するには それから私はこのよう呼び出そう:
smp('body');
が、ブラウザが私のSMPの定義を見つけることができません。将来的には
私は(例えば、色を変更する)このようにそれを使用するには、いくつかのメソッドを追加したい:
smp('.myClass').method('attr')
誰かが私に私のミスを示すことができる場合、私は、感謝します。
UPD:
(function color(smp) {
window.smp.prototype.changeColor = function() {
for (var i = 0; i < this.length; i++)
this.el[i].style.color = color;
return this;
};
})(smp);
は 'selector'が文字列である...文字列は'何のプロパティを持っていないnodeType'実際にはこれが
jQuery
を含むほとんどのライブラリは、何をすべきかです – charlietfl