需要幫助。我有一個名爲Rigged的庫,類似於jQuery。 此代碼是不完整的,這只是我的代碼示例(我的lib中擁有超過500線尚)在同一對象中使用對象的方法
(function() {
var arr = [];
var Rigged = function (selector) {
return Rigged.fn.init(selector);
};
Rigged.fn = Rigged.prototype = {
map: function (callback) {
var results = arr, i = 0;
for (; i < this.length; i++) {
results.push(callback.call(this, this[i], i));
}
return results;
},
each: function (callback) {
return this.map(callback);
},
// this is just example of my code
css: function (attr, value) {
if (attr == 'display') {
return this.each(function (current) {
current.style.display = value;
});
}
},
hide: function() {
return this.each(function (current) {
// here's a problem
current.css('display', 'none');
});
}
};
Rigged.init = Rigged.fn.init = function (selector) {
var elset, i = 0;
if (typeof selector === "string")
elset = document.querySelectorAll(selector);
else if (...)
.... etc
this.length = elset.length;
for (; i < this.length; i++) { this[i] = elset[i]; }
return this;
}
Rigged.ajax = Rigged.fn.ajax = function (obj) {
// code here
}
window.Rigged = window.$ = Rigged;
}());
所以,我有打電話map
方法或each
沒有問題,但在方法的定義,所謂hide
它打印一個錯誤Uncaught TypeError:current.css不是控制檯中的函數。
當我在$("#text").css('display', 'none);
這樣的索引文件中調用css
時,它有效,但在Rigged.prototype
中不起作用。當我將current.css('display', 'none');
替換爲current.style.display = 'none';
時,它正常工作。
有誰能告訴我爲什麼.css
方法不起作用嗎?
EDITED .MAP()方法 + E(回調)到當前
那麼'.map'方法錯了?我無法使用它?我是否應該只在其他方法中使用'this'? – Peter
@Peter'.map()'沒有錯,但我認爲沒有必要使用它,它只是加載一個循環。 '.each()'就足夠你的目的。 – thecodeparadox
好的..非常感謝 – Peter