压缩点击事件/函数
有更好的方法来编码吗?我有大约 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
添加
将
data-selectid="X"
到
.selectX
元素中,其中 x 是最后一个数字,并添加新类 - 选择add
data-selectid="X"
into your
.selectX
elements where x is that last number and also add new class - selects