简体   繁体   English

如何缩短这个jQuery函数

[英]how to shorten this jquery function

If I need to do this for every picture that expands or link, how can I say combine this statement using five different classes. 如果我需要为每个扩展或链接的图片执行此操作,该如何使用五个不同的类将该语句组合起来。 Ex. 例如 .img1, .img2, .img3, .img4, .img5 Instead of repeating this function for every class, I would like to combine them all if possible. .img1,.img2,.img3,.img4,.img5除了在每个类上都重复此功能之外,我想尽可能地将它们组合在一起。 Checkout the plus picture links at the top of my site to see what I am talking about. 在我的网站顶部查看加号图片链接,以了解我在说什么。 http://www.stephencarlwillis.com In order to understand please see this link first with my first question. http://www.stephencarlwillis.com为了理解,请首先参阅此链接以及我的第一个问题。 switch image from plus to minus when user clicks image 用户单击图像时将图像从加号切换为减号

$(document).ready(function () {
    var open = true;
    $('.img2').click(function () {
        $(this).attr('src', function (i, oldSrc) {
            return oldSrc == 'assets/images/close.png' ? 'assets/images/open.png' : 'assets/images/close.png';
        });

        if (open == true) {
            $('#right2').toggle("fast");
            $('#development').toggle("fade");

            open = true;
        } else {
            return false;

            open = false;
        }

    });
});

如何为每个图像添加另一个类,例如:“ imgPlus”,然后:$('。imgPlus')。click(function ...........

Just add another class to the images.. 只需向图像添加另一个类。

Ie.... .images 即....图像

Then just add that as your click handler. 然后只需将其添加为点击处理程序即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM