标签:
场景:为多个页面写了公用的方法,加载到不同的元素里,一般写法是:
if($(‘.A‘).lenght){
$(‘.A‘).method();
}
if($(‘.B‘).lenght){
$(‘.B‘).method();
}
if($(‘.C‘).lenght){
$(‘.C‘).method();
}
得写多个判断,再执行,比较繁琐。。
可用jq的扩展方法来实现 更便捷:
$.fn.loadList = function(){
// 代码
var method = function(){
// 代码
}
return this.each(function(){
method();
});
}
$(‘.A‘).loadList();
$(‘.B‘).loadList();
$(‘.C‘).loadList();
这样封装好。。
标签:
原文地址:http://www.cnblogs.com/damade/p/5109165.html