将类似的单击事件操作转换为函数?

时间:2012-02-15 14:27:06

标签: javascript jquery function

我有4个点击事件,使用几乎相同的跟踪代码,所以我认为这个代码会更好地驻留在一个函数中,我想知道的是我如何设置与事件不同的某些值事件?我是否需要将参数传递给具有值的新函数?

我的点击事件代码是:

$('a.close', '#contents').on('click', function(){
    s=s_gi('myapp');
    s.linkTrackVars="None"; 
    s.linkTrackEvents="None";
    s.tl(this,'o','myapp_fb:new_story:a');
});

$('a.next', '#contents').on('click', function(){
    s=s_gi('myapp');
    s.linkTrackVars="None"; 
    s.linkTrackEvents="None";
    s.tl(this,'o','myapp_fb:new_story:b');
});

$('a.prev', '#contents').on('click', function(){
    s=s_gi('myapp');
    s.linkTrackVars="None"; 
    s.linkTrackEvents="None";
    s.tl(this,'o','myapp_fb:new_story:c');
}):

$('a.share', '#contents').on('click', function(){
    s=s_gi('myapp');
    s.linkTrackVars="None"; 
    s.linkTrackEvents="None";
    s.tl(this,'o','myapp_fb:new_story:d');
});

1 个答案:

答案 0 :(得分:2)

你可以这样做:

var clickHandler = function(value) {
    s=s_gi('myapp');
    s.linkTrackVars="None"; 
    s.linkTrackEvents="None";
    s.tl(this,'o',value);
}

$('a.close', '#contents').on('click', function(){
    clickHandler('myapp_fb:new_story:a');
});