jQuery链接:一切都可以链接吗?我们什么时候不能连锁? [英] jQuery chaining: Can everything be chained? When can we not chain?
问题描述
我知道不是所有的jQuery函数都可以链接在一起.在这方面有经验法则吗?我们什么时候不能将2个功能链接在一起.
I know that not all jQuery functions can be chained together. Is there a rule of thumb on this. When can we not chain 2 functions together.
推荐答案
您可以在函数返回"jQuery对象"时进行链接.
You can chain when the function returns a "jQuery object".
例如, .css(property, value)
可以链接在一起,因为文档说它返回jQuery :
For example, .css(property, value)
can be chained, as the doc says it Returns jQuery:
而> .height()
不能,因为它返回一个整数.
while .height()
cannot, because it returns an integer.
通常,返回"jQuery对象"的函数通常是不会返回值"的函数,例如设置方法(.css(prop, val)
,.addClass()
),事件绑定器(.click(handler)
)等.
Typically, the functions that returns "jQuery objects" are those which typically would not "return a value", e.g. setter methods (.css(prop, val)
, .addClass()
), event binders (.click(handler)
), etc.
(当然也可以链接遍历方法(.parent()
,.find()
等),但是返回的对象将与输入的对象不同.)
(Of course traverse methods (.parent()
, .find()
, etc.) can also be chained but the returned object will be different from the input.)
这篇关于jQuery链接:一切都可以链接吗?我们什么时候不能连锁?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!