如何编写可重用的jquery click函数? [英] How to write a reusable jquery click function?

查看:95
本文介绍了如何编写可重用的jquery click函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个手风琴功能:

 $('.accordion a').click(function () {

        if($(this).is('.accordionClose')) {
            $('.accordionOpen').toggleClass('accordionOpen').toggleClass('accordionClose').next().slideToggle().toggleClass('openContent');
            $(this).toggleClass('accordionOpen').toggleClass('accordionClose');
            $(this).next().slideToggle().toggleClass('openContent');
        }

        else {
            $(this).toggleClass('accordionOpen').toggleClass('accordionClose');
            $(this).next().slideToggle().toggleClass('openContent');
        }
    });

我有这个片段来制作手风琴效果.问题是我不知道如何使它成为通用函数并在需要时调用它.我是否需要以插件格式制作它? 或其他任何可行的方式?

I have this snippet to make a accordion effect. Problem is I am not aware how to make this a common function and call it whenever required. Do I need to make it in a plugin format ? Or any other possible way of doing it?

我知道使用多个用逗号分隔的选择器.我不是那个意思.

I am aware of using multiple selctors separated by comma. I do not mean that.

推荐答案

这样写:

function func_name(){
//do stuff here
}

//call this later like this:

$('.accordion a').click(func_name);

//Or call like this:
$('.accordion a').click(function(){
  func_name(); //parenthesis required here
});

此外,您可以查看另一篇帖子,以了解更多内容.

Also, you may look at another post for stepping up a little more.

这篇关于如何编写可重用的jquery click函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆