由于我学习了jQuery,我总是想知道jQuery如何通过添加点.
来执行一个接一个的函数(不知道它的真名,对不起);
$("#somediv").fadeIn("fast").css("background","blue");
当淡入淡出效果完成时,CSS函数执行。就像你可以一个接一个地执行你想要的任何功能一样。
我该怎么做?
注意:如果我说错了,请纠正我,我只是想学习。
答案 0 :(得分:5)
它返回相同类型的对象,这是演示该技术的一个非常简单的例子:
var Counter = function(){
this.val = 0;
};
Counter.prototype.increment = function(){
++this.val;
return this;
};
Counter.prototype.decrement = function(){
--this.val;
return this;
};
Counter.prototype.log = function(){
console.log(this.val);
return this;
};
var o = new Counter();
o.increment().increment().decrement().log().increment().log();
答案 1 :(得分:3)
它被称为method chaining,其中一个方法返回它所称的对象。
您还可以参考主题上的以下帖子
How can jQuery do method chaining
how does jquery chaining work?
答案 2 :(得分:0)
简短的回答非常简单。每个方法返回与传递给它的选择器匹配的元素集合,允许将相同的集合传递给下一个链式方法。
查看return this