如果我坚持使用原型而不是jQuery,但是我喜欢jQuery的链式能力,那么通过以下方式为CSS选择器带来这种功能有什么坏处吗?
function r(f) {
return function() {
var args = [];
for(var i = 0; i < arguments.length; i++) {
args.push(arguments[i]);
}
this.each(function(e) {
if (e && (typeof e[f] == 'function' || typeof e[f] == 'object'))
e[f].apply(e, args);
});
return this;
}
}
function om(p1, p2) {
for( prop in p1 ) {
if( p1.hasOwnProperty( prop ) ){
p2[prop] = r(prop);
}
}
}
om(Element.Methods, Array.prototype);这里隐藏着一些改进/增强。但是,代码有什么问题吗?还是我完全遗漏了什么?
由于使用了这段代码,现在我可以执行以下操作.
$$('.something').html('blah').show();一个例子:http://jsbin.com/olojo5/10/edit
发布于 2011-06-08 14:50:04
在Prototype.js中进行“链接”的标准方法是使用"each“显式迭代选择器返回的元素数组。例如:
$$(".something").each(function(e){e.update("foo").show();});话虽如此,我认为您的代码没有任何问题,只要它符合您的编码风格!
https://stackoverflow.com/questions/6217836
复制相似问题