[英]jQuery and chaining
Sometimes I need to use .off() before .on(). 有时我需要在.on()之前使用.off()。
My Question is can I chain them both together or should they remain separate? 我的问题是我可以将它们链接在一起还是应该保持分开?
Can this: 可以这样:
$('button').off('click');
$('button;).on('click, function() {
// code here.
});
Become this and work the same: 成为这个,并且工作相同:
$('button').off('click').on('click', function() {
// code here
});
Will the .off() execute before the .on() in the chain. .off()是否在链中的.on()之前执行。
I'm fairly new to chaining this is why I ask. 我对链接尚不陌生,这就是我要问的原因。
Yes, it will execute before the .on()
method. 是的,它将在
.on()
方法之前执行。 Basically chaining allows you to write less amount of code and, btw, it fasters it a bit, cause you don't have to find your element multiple times in the DOM tree.(Each time you call $('#yourElmId')
you basically call document.getElementById
so if you call it twice - you will perform search of it two times and for big projects it's not great at all) 基本上,链接使您可以编写更少的代码,并且顺便说一下,它可以更快地执行代码,因为您不必在DOM树中多次查找元素。(每次调用
$('#yourElmId')
基本上调用document.getElementById
所以如果您两次调用它-您将对其进行两次搜索,对于大型项目来说根本不好用)
More details on jquery chaining here 有关jQuery链接的更多详细信息,请点击此处
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.