gpt4 book ai didi

javascript - 压缩 Javascript/jQuery 代码

转载 作者:行者123 更新时间:2023-11-28 11:26:17 25 4
gpt4 key购买 nike

首先,我要感谢任何可以帮助我压缩这段 Javascript/jQuery 代码的人。

        jQuery(function() {

jQuery('#pitem-1').click(function(e) {
jQuery("#image-1").lightbox_me({centered: true, onLoad: function() {
jQuery("#image-1").find("input:first").focus();
}});

e.preventDefault();
});

jQuery('#pitem-2').click(function(e) {
jQuery("#image-2").lightbox_me({centered: true, onLoad: function() {
jQuery("#image-2").find("input:first").focus();
}});

e.preventDefault();
});

jQuery('#pitem-3').click(function(e) {
jQuery("#image-3").lightbox_me({centered: true, onLoad: function() {
jQuery("#image-3").find("input:first").focus();
}});

e.preventDefault();
});

jQuery('table tr:nth-child(even)').addClass('stripe');
});

基本上每个#pitem-ID都会在弹出窗口中打开相同的#image-ID。

再次感谢任何可以提供帮助的人。

jack

最佳答案

您的函数看起来几乎相同,这表明您应该将该功能移至可以调用的函数中:

function createHandler(id) {
return function (e) {
$(id).lightbox_me({centered: true, onLoad: function() {
$(id).find("input:first").focus();
}});

e.preventDefault();
}
};

然后你可以使用:

 $('#pitem-2').bind('click', createHandler("#image-2"));

关于javascript - 压缩 Javascript/jQuery 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10359594/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com