【发布时间】:2015-05-10 22:00:46
【问题描述】:
我正在玩 jQuery 插件开发,我想链接方法。我在 jQuery 教程 (https://learn.jquery.com/plugins/basic-plugin-creation/) 中读到,您可以通过在方法末尾添加 return this; 来链接方法,这适用于第一种方法(测试 1)。对于使用console.log 的第二种方法(测试2),我该如何做到这一点?所有方法都可以链接吗?
// test 1
$.fn.greenify = function () {
this.css('color', 'green');
return this;
};
// test 2
$.fn.console = function () {
this.on('click', function () {
console.log('hello world');
});
};
$('a').greenify().console();
【问题讨论】:
标签: javascript jquery plugins chaining method-chaining