【发布时间】:2015-06-10 10:49:33
【问题描述】:
我有这些“功能”,这些“功能”在块中针对各种元素重复。 如何简化使用“var”?
谢谢你:)
例如:
$('#test1').waypoint(function (direction) {
if (direction === 'down') {
$(this).addClass("here");
$(this).prevAll().removeClass("here");
$(this).prev().prev().addClass("here_pre");
$(this).next().next().addClass("here_pre");
},
});
我想达成这样的解决方案:
var active_here = $(this).addClass("here"),
$(this).prevAll().removeClass("here"),
$(this).prev().prev().addClass("here_pre"),
$(this).next().next().addClass("here_pre");
最后回忆一下这样的事情:
$('#test1').waypoint(function (direction) {
if (direction === 'down') {
active_here;
},
});
$('#test2').waypoint(function (direction) {
if (direction === 'up') {
active_here;
},
});
etc... etc... etc...
【问题讨论】:
-
您是在问如何编写自己的函数?
标签: function var light simplify