Ami*_*ole 5 jquery function chainable
我的函数根据data属性返回一个过滤的(数组)项列表.
如果我可以将这个功能链接起来,我想要它:
$(document).ready(function (){
function filterSvcType (svcType) {
var selectedServices = $("#service-list div");
var chose = selectedServices.filter(function() {
return $(this).data("service-type") == svcType;
});
console.log(chose);
}
filterSvcType("hosting");
});
Run Code Online (Sandbox Code Playgroud)
我想要做的是这样称呼它:
filterSvcType("hosting").fadeOut();
Run Code Online (Sandbox Code Playgroud)
我该怎么做呢?
您需要添加的只是return chose;
在console.log
通话后.
但你也可以把它变成一个jQuery插件
(function($) {
$.fn.filterServiceType = function(svcType){
return this.filter(function(){
return $(this).data("service-type") == svcType;
});
};
})(jQuery);
Run Code Online (Sandbox Code Playgroud)
然后你可以打电话给
$('#service-list div').filterSvcType('hosting').fadeOut();
Run Code Online (Sandbox Code Playgroud)
哪个更jQueryish.