压缩点击事件/函数

发布于 2024-12-05 00:07:29 字数 2883 浏览 1 评论 0原文

有更好的方法来编码吗?我有大约 100 个项目,每个项目都有点击事件。有没有办法使用 .delegate,或者只是更好的方法来处理这个问题?这段代码也按照我想要的方式工作,但我希望它更加动态。基本上,单击 div 中的类 div 后,我希望将整个父 div 克隆到另一个 div 中。如果需要更多信息,请告诉我。

谢谢! 迈克尔

$(function() { 
        $(".select1").click(function () {
            if ($('.saved_list .thumb1').length == 0 && $('.saved > li').length < totalPic)
                {
                    $(this).parent().clone().appendTo('.saved_list ul');
                    $('.saved .select1').replaceWith('<div class="remove">Remove</div>');
                    $('.saved a').contents().unwrap();
                    //alert($('.saved > li').length);
                } else {
                alert ('You can only choose 3 paintings');  
                }
        });

        $(".select2").click(function () {
            if ($('.saved_list .thumb2').length == 0 && $('.saved > li').length < totalPic)
                {
                    $(this).parent().clone().appendTo('.saved_list ul');
                    $('.saved .select2').replaceWith('<div class="remove">Remove</div>');
                    $('.saved a').contents().unwrap();
                    //alert (ct);
                } else {
                alert ('You can only choose 3 paintings');  
                }
        });

        $(".select3").click(function () {
            if ($('.saved_list .thumb3').length == 0 && $('.saved > li').length < totalPic)
                {
                    $(this).parent().clone().appendTo('.saved_list ul');
                    $('.saved .select3').replaceWith('<div class="remove">Remove</div>');
                    $('.saved a').contents().unwrap();
                } else {
                alert ('You can only choose 3 paintings');  
                }
        });

        $(".select4").click(function () {
            if ($('.saved_list .thumb4').length == 0 && $('.saved > li').length < totalPic)
                {
                    $(this).parent().clone().appendTo('.saved_list ul');
                    $('.saved .select4').replaceWith('<div class="remove">Remove</div>');
                    $('.saved a').contents().unwrap();
                } else {
                alert ('You can only choose 3 paintings');  
                }
        });


        $(".select5").click(function () {
            if ($('.saved_list .thumb5').length == 0 && $('.saved > li').length < totalPic)
                {
                    $(this).parent().clone().appendTo('.saved_list ul');
                    $('.saved .select5').replaceWith('<div class="remove">Remove</div>');
                    $('.saved a').contents().unwrap();
                } else {
                alert ('You can only choose 3 paintings');  
                }
        }); 
}); 

Is there a better way to code this? I have around 100 items that each have click events. Is there a way to use .delegate, or just a better way to handle this? This code works the way I want it too but I would like it to be more dynamic. Basically a class div within a div is clicked and I want the whole parent div to be cloned into another div once it is selected. Please let me know if more information is needed.

Thanks!
Michael

$(function() { 
        $(".select1").click(function () {
            if ($('.saved_list .thumb1').length == 0 && $('.saved > li').length < totalPic)
                {
                    $(this).parent().clone().appendTo('.saved_list ul');
                    $('.saved .select1').replaceWith('<div class="remove">Remove</div>');
                    $('.saved a').contents().unwrap();
                    //alert($('.saved > li').length);
                } else {
                alert ('You can only choose 3 paintings');  
                }
        });

        $(".select2").click(function () {
            if ($('.saved_list .thumb2').length == 0 && $('.saved > li').length < totalPic)
                {
                    $(this).parent().clone().appendTo('.saved_list ul');
                    $('.saved .select2').replaceWith('<div class="remove">Remove</div>');
                    $('.saved a').contents().unwrap();
                    //alert (ct);
                } else {
                alert ('You can only choose 3 paintings');  
                }
        });

        $(".select3").click(function () {
            if ($('.saved_list .thumb3').length == 0 && $('.saved > li').length < totalPic)
                {
                    $(this).parent().clone().appendTo('.saved_list ul');
                    $('.saved .select3').replaceWith('<div class="remove">Remove</div>');
                    $('.saved a').contents().unwrap();
                } else {
                alert ('You can only choose 3 paintings');  
                }
        });

        $(".select4").click(function () {
            if ($('.saved_list .thumb4').length == 0 && $('.saved > li').length < totalPic)
                {
                    $(this).parent().clone().appendTo('.saved_list ul');
                    $('.saved .select4').replaceWith('<div class="remove">Remove</div>');
                    $('.saved a').contents().unwrap();
                } else {
                alert ('You can only choose 3 paintings');  
                }
        });


        $(".select5").click(function () {
            if ($('.saved_list .thumb5').length == 0 && $('.saved > li').length < totalPic)
                {
                    $(this).parent().clone().appendTo('.saved_list ul');
                    $('.saved .select5').replaceWith('<div class="remove">Remove</div>');
                    $('.saved a').contents().unwrap();
                } else {
                alert ('You can only choose 3 paintings');  
                }
        }); 
}); 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暖树树初阳… 2024-12-12 00:07:29

添加

data-selectid="X"

.selectX 元素中,其中 x 是最后一个数字,并添加新类 - 选择

$(function() { 
        $(".selects").click(function () {
            var id = $(this).data('selectid');
            if ($('.saved_list .thumb'+id).length == 0 && $('.saved > li').length < totalPic)
                {
                    $(this).parent().clone().appendTo('.saved_list ul');
                    $('.saved .select'+id).replaceWith('<div class="remove">Remove</div>');
                    $('.saved a').contents().unwrap();
                    //alert($('.saved > li').length);
                } else {
                alert ('You can only choose 3 paintings');  
                }
        });


}); 

add

data-selectid="X"

into your .selectX elements where x is that last number and also add new class - selects

$(function() { 
        $(".selects").click(function () {
            var id = $(this).data('selectid');
            if ($('.saved_list .thumb'+id).length == 0 && $('.saved > li').length < totalPic)
                {
                    $(this).parent().clone().appendTo('.saved_list ul');
                    $('.saved .select'+id).replaceWith('<div class="remove">Remove</div>');
                    $('.saved a').contents().unwrap();
                    //alert($('.saved > li').length);
                } else {
                alert ('You can only choose 3 paintings');  
                }
        });


}); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文