简化我的 jQuery 代码,该代码变得越来越庞大和冗余

发布于 2024-08-28 09:39:46 字数 6891 浏览 4 评论 0原文

我不是 jQuery 专家,但我正在学习。我正在使用一点(增长到很多)jQuery 来隐藏一些图像并在单击拇指时显示单个图像。虽然 jQuery 的这一点可以工作,但它的效率非常低,但我不确定如何将其简化为在更通用的级别上工作的东西。

<script>


$(document).ready(function () {

// Changing the Materials
$("a#shirtred").click(function () {
    $("#selectMaterials img").removeClass("visible");
    $("img.selectShirtRed").addClass("visible");
});

$("a#shirtgrey").click(function () {
    $("#selectMaterials img").removeClass("visible");
    $("img.selectShirtGrey").addClass("visible");
});

$("a#shirtgreen").click(function () {
    $("#selectMaterials img").removeClass("visible");
    $("img.selectShirtGreen").addClass("visible");
});

$("a#shirtblue").click(function () {
    $("#selectMaterials img").removeClass("visible");
    $("img.selectShirtBlue").addClass("visible");
});

// Changing the Collars
$("a#collarred").click(function () {
    $("#selectCollar img").removeClass("visible");
    $("img.selectCollarRed").addClass("visible");
});

$("a#collargrey").click(function () {
    $("#selectCollar img").removeClass("visible");
    $("img.selectCollarGrey").addClass("visible");
});

$("a#collargreen").click(function () {
    $("#selectCollar img").removeClass("visible");
    $("img.selectCollarGreen").addClass("visible");
});

$("a#collarblue").click(function () {
    $("#selectCollar img").removeClass("visible");
    $("img.selectCollarBlue").addClass("visible");
});

// Changing the Cuffs
$("a#cuffred").click(function () {
    $("#selectCuff img").removeClass("visible");
    $("img.selectCuffRed").addClass("visible");
});

$("a#cuffgrey").click(function () {
    $("#selectCuff img").removeClass("visible");
    $("img.selectCuffGrey").addClass("visible");
});

$("a#cuffblue").click(function () {
    $("#selectCuff img").removeClass("visible");
    $("img.selectCuffBlue").addClass("visible");
});

$("a#cuffgreen").click(function () {
    $("#selectCuff img").removeClass("visible");
    $("img.selectCuffGreen").addClass("visible");
});

// Changing the Pockets
$("a#pocketred").click(function () {
    $("#selectPocket img").removeClass("visible");
    $("img.selectPocketRed").addClass("visible");
});

$("a#pocketgrey").click(function () {
    $("#selectPocket img").removeClass("visible");
    $("img.selectPocketGrey").addClass("visible");
});

$("a#pocketblue").click(function () {
    $("#selectPocket img").removeClass("visible");
    $("img.selectPocketBlue").addClass("visible");
});

$("a#pocketgreen").click(function () {
    $("#selectPocket img").removeClass("visible");
    $("img.selectPocketGreen").addClass("visible");
});

});
</scrip>

<!-- Thumbnails which can be clicked on to toggle the larger preview image -->


        <div class="materials">
    <a href="javascript:;" id="shirtgrey"><img src="/grey_shirt.png" height="122" width="122" /></a> 
    <a href="javascript:;" id="shirtred"><img src="red_shirt.png" height="122" width="122" /></a> 
    <a href="javascript:;" id="shirtblue"><img src="hblue_shirt.png" height="122" width="122" /></a> 
    <a href="javascript:;" id="shirtgreen"><img src="green_shirt.png" height="122" width="122" /></a> 
    </div>


    <div class="collars">
    <a href="javascript:;" id="collargrey"><img  src="grey_collar.png" height="122" width="122" /></a> 
    <a href="javascript:;" id="collarred"><img  src="red_collar.png" height="122" width="122" /></a> 
    <a href="javascript:;" id="collarblue"><img  src="blue_collar.png" height="122" width="122" /></a> 
    <a href="javascript:;" id="collargreen"><img  src="green_collar.png" height="122" width="122" /></a>
    </div>

    <div class="cuffs">
    <a href="javascript:;" id="cuffgrey"><img  src="grey_cuff.png" height="122" width="122" /></a> 
    <a href="javascript:;" id="cuffred"><img  src="red_cuff.png" height="122" width="122" /></a> 
    <a href="javascript:;" id="cuffblue"><img  src="blue_cuff.png" height="122" width="122" /></a> 
    <a href="javascript:;" id="cuffgreen"><img  src="/green_cuff.png" height="122" width="122" /></a>
    </div>

    <div class="pockets">
    <a href="javascript:;" id="pocketgrey"><img  src="grey_pocket.png" height="122" width="122" /></a> 
    <a href="javascript:;" id="pocketred"><img  src=".png" height="122" width="122" /></a> 
    <a href="javascript:;" id="pocketblue"><img  src="blue_pocket.png" height="122" width="122" /></a> 
    <a href="javascript:;" id="pocketgreen"><img  src="green_pocket.png" height="122" width="122" /></a>
    </div>

<!-- The larger images where one from each set should be viewable at one time, triggered by the thumb clicked above -->


        <div class="selectionimg"> 
        <div id="selectShirt">
        <img src="grey_shirt.png" height="250" width="250" class="selectShirtGrey show" />        
        <img src="red_shirt.png" height="250" width="250" class="selectShirtRed hide" />        
        <img src="blue_shirt.png" height="250" width="250" class="selectShirtBlue hide" />        
        <img src="green_shirt.png" height="250" width="250" class="selectShirtGreen hide" />  </div>

         <div id="selectCollar">
        <img src="grey_collar.png" height="250" width="250" class="selectCollarGrey show" />        
        <img src="red_collar.png" height="250" width="250" class="selectCollarRed hide" />        
        <img src="blue_collar.png" height="250" width="250" class="selectCollarBlue hide" />        
        <img src="green_collar.png" height="250" width="250" class="selectCollarGreen hide" />       </div>

         <div id="selectCuff">
        <img src="grey_cuff.png" height="250" width="250" class="selectCuffGrey show" />        
        <img src="red_cuff.png" height="250" width="250" class="selectCuffRed hide" />        
        <img src="blue_cuff.png" height="250" width="250" class="selectCuffBlue hide" />        
        <img src="green_cuff.png" height="250" width="250" class="selectCuffGreen hide" />     </div>

         <div id="selectPocket">
        <img src="grey_pocket.png" height="250" width="250" class="selectPocketGrey show" />        
        <img src="hred_pocket.png" height="250" width="250" class="selectPocketRed hide" />        
        <img src="blue_pocket.png" height="250" width="250" class="selectPocketBlue hide" />        
        <img src="green_pocket.png" height="250" width="250" class="selectPocketGreen hide" />   
    </div>     </div>

I am no jQuery expert, but I'm learning. I'm using a bit (growing to a LOT) of jQuery to hide some images and show a single image when a thumb is clicked. While this bit of jQuery works, it's horribly inefficient but I am unsure of how to simplify this to something that works on more of a universal level.

<script>


$(document).ready(function () {

// Changing the Materials
$("a#shirtred").click(function () {
    $("#selectMaterials img").removeClass("visible");
    $("img.selectShirtRed").addClass("visible");
});

$("a#shirtgrey").click(function () {
    $("#selectMaterials img").removeClass("visible");
    $("img.selectShirtGrey").addClass("visible");
});

$("a#shirtgreen").click(function () {
    $("#selectMaterials img").removeClass("visible");
    $("img.selectShirtGreen").addClass("visible");
});

$("a#shirtblue").click(function () {
    $("#selectMaterials img").removeClass("visible");
    $("img.selectShirtBlue").addClass("visible");
});

// Changing the Collars
$("a#collarred").click(function () {
    $("#selectCollar img").removeClass("visible");
    $("img.selectCollarRed").addClass("visible");
});

$("a#collargrey").click(function () {
    $("#selectCollar img").removeClass("visible");
    $("img.selectCollarGrey").addClass("visible");
});

$("a#collargreen").click(function () {
    $("#selectCollar img").removeClass("visible");
    $("img.selectCollarGreen").addClass("visible");
});

$("a#collarblue").click(function () {
    $("#selectCollar img").removeClass("visible");
    $("img.selectCollarBlue").addClass("visible");
});

// Changing the Cuffs
$("a#cuffred").click(function () {
    $("#selectCuff img").removeClass("visible");
    $("img.selectCuffRed").addClass("visible");
});

$("a#cuffgrey").click(function () {
    $("#selectCuff img").removeClass("visible");
    $("img.selectCuffGrey").addClass("visible");
});

$("a#cuffblue").click(function () {
    $("#selectCuff img").removeClass("visible");
    $("img.selectCuffBlue").addClass("visible");
});

$("a#cuffgreen").click(function () {
    $("#selectCuff img").removeClass("visible");
    $("img.selectCuffGreen").addClass("visible");
});

// Changing the Pockets
$("a#pocketred").click(function () {
    $("#selectPocket img").removeClass("visible");
    $("img.selectPocketRed").addClass("visible");
});

$("a#pocketgrey").click(function () {
    $("#selectPocket img").removeClass("visible");
    $("img.selectPocketGrey").addClass("visible");
});

$("a#pocketblue").click(function () {
    $("#selectPocket img").removeClass("visible");
    $("img.selectPocketBlue").addClass("visible");
});

$("a#pocketgreen").click(function () {
    $("#selectPocket img").removeClass("visible");
    $("img.selectPocketGreen").addClass("visible");
});

});
</scrip>

<!-- Thumbnails which can be clicked on to toggle the larger preview image -->


        <div class="materials">
    <a href="javascript:;" id="shirtgrey"><img src="/grey_shirt.png" height="122" width="122" /></a> 
    <a href="javascript:;" id="shirtred"><img src="red_shirt.png" height="122" width="122" /></a> 
    <a href="javascript:;" id="shirtblue"><img src="hblue_shirt.png" height="122" width="122" /></a> 
    <a href="javascript:;" id="shirtgreen"><img src="green_shirt.png" height="122" width="122" /></a> 
    </div>


    <div class="collars">
    <a href="javascript:;" id="collargrey"><img  src="grey_collar.png" height="122" width="122" /></a> 
    <a href="javascript:;" id="collarred"><img  src="red_collar.png" height="122" width="122" /></a> 
    <a href="javascript:;" id="collarblue"><img  src="blue_collar.png" height="122" width="122" /></a> 
    <a href="javascript:;" id="collargreen"><img  src="green_collar.png" height="122" width="122" /></a>
    </div>

    <div class="cuffs">
    <a href="javascript:;" id="cuffgrey"><img  src="grey_cuff.png" height="122" width="122" /></a> 
    <a href="javascript:;" id="cuffred"><img  src="red_cuff.png" height="122" width="122" /></a> 
    <a href="javascript:;" id="cuffblue"><img  src="blue_cuff.png" height="122" width="122" /></a> 
    <a href="javascript:;" id="cuffgreen"><img  src="/green_cuff.png" height="122" width="122" /></a>
    </div>

    <div class="pockets">
    <a href="javascript:;" id="pocketgrey"><img  src="grey_pocket.png" height="122" width="122" /></a> 
    <a href="javascript:;" id="pocketred"><img  src=".png" height="122" width="122" /></a> 
    <a href="javascript:;" id="pocketblue"><img  src="blue_pocket.png" height="122" width="122" /></a> 
    <a href="javascript:;" id="pocketgreen"><img  src="green_pocket.png" height="122" width="122" /></a>
    </div>

<!-- The larger images where one from each set should be viewable at one time, triggered by the thumb clicked above -->


        <div class="selectionimg"> 
        <div id="selectShirt">
        <img src="grey_shirt.png" height="250" width="250" class="selectShirtGrey show" />        
        <img src="red_shirt.png" height="250" width="250" class="selectShirtRed hide" />        
        <img src="blue_shirt.png" height="250" width="250" class="selectShirtBlue hide" />        
        <img src="green_shirt.png" height="250" width="250" class="selectShirtGreen hide" />  </div>

         <div id="selectCollar">
        <img src="grey_collar.png" height="250" width="250" class="selectCollarGrey show" />        
        <img src="red_collar.png" height="250" width="250" class="selectCollarRed hide" />        
        <img src="blue_collar.png" height="250" width="250" class="selectCollarBlue hide" />        
        <img src="green_collar.png" height="250" width="250" class="selectCollarGreen hide" />       </div>

         <div id="selectCuff">
        <img src="grey_cuff.png" height="250" width="250" class="selectCuffGrey show" />        
        <img src="red_cuff.png" height="250" width="250" class="selectCuffRed hide" />        
        <img src="blue_cuff.png" height="250" width="250" class="selectCuffBlue hide" />        
        <img src="green_cuff.png" height="250" width="250" class="selectCuffGreen hide" />     </div>

         <div id="selectPocket">
        <img src="grey_pocket.png" height="250" width="250" class="selectPocketGrey show" />        
        <img src="hred_pocket.png" height="250" width="250" class="selectPocketRed hide" />        
        <img src="blue_pocket.png" height="250" width="250" class="selectPocketBlue hide" />        
        <img src="green_pocket.png" height="250" width="250" class="selectPocketGreen hide" />   
    </div>     </div>

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

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

发布评论

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

评论(5

冷情 2024-09-04 09:39:46
$("a[color]").each(function() {        
    $(this).click(function () {
        var color = $(this).attr('color');
        $("#selectCuff img").removeClass("visible");
        $("img[color="+color+"]").addClass("visible");
    });
});

就这样?

您还可以使用这样的上下文 css 类

#select img { 
    display:none;
}
#select.red img.red{
    display:inline;
}

,并在 #select div 上添加/删除颜色类

我刚刚意识到您甚至不需要

$("a[color]").click(function() {                
    var color = $(this).attr('color');
    $("#selectCuff img").removeClass("visible");
    $("img[color="+color+"]").addClass("visible");
});

标记中的“each”

<a href="#" color="grey">show grey</a>
<div id="select">
<img src="grey_collar.png" height="250" width="250" color="grey" />
<img src="red_collar.png" height="250" width="250" color="red" />
</div>
$("a[color]").each(function() {        
    $(this).click(function () {
        var color = $(this).attr('color');
        $("#selectCuff img").removeClass("visible");
        $("img[color="+color+"]").addClass("visible");
    });
});

like that?

Also you can play with context css classes like that

#select img { 
    display:none;
}
#select.red img.red{
    display:inline;
}

and add/remove color class on #select div

I've just realize that you don't even need 'each' here

$("a[color]").click(function() {                
    var color = $(this).attr('color');
    $("#selectCuff img").removeClass("visible");
    $("img[color="+color+"]").addClass("visible");
});

in markup

<a href="#" color="grey">show grey</a>
<div id="select">
<img src="grey_collar.png" height="250" width="250" color="grey" />
<img src="red_collar.png" height="250" width="250" color="red" />
</div>
才能让你更想念 2024-09-04 09:39:46

你可以做这样的事情吗? (检查正确的情况等..未测试)

<script> 

function setupMaterialsClick(name)
{
   $("a#" + name).click(function () { 
       $("#selectMaterials img").removeClass("visible"); 
       $("img.select" + name).addClass("visible"); 
   }); 
}

$(document).ready(function () 
{ 

   // Changing the Materials 
   setupMaterialsClick("shirtred");
   // etc

   // Other bits

}); 

}); 
</script>

You could do something like this? (check for corrct case etc..not tested)

<script> 

function setupMaterialsClick(name)
{
   $("a#" + name).click(function () { 
       $("#selectMaterials img").removeClass("visible"); 
       $("img.select" + name).addClass("visible"); 
   }); 
}

$(document).ready(function () 
{ 

   // Changing the Materials 
   setupMaterialsClick("shirtred");
   // etc

   // Other bits

}); 

}); 
</script>
过去的过去 2024-09-04 09:39:46

更改您的类以匹配链接 id 的大小写,然后执行以下操作:

$('materials a').bind(changeSelection);
$('collars a').bind(changeSelection);
$('cuffs a').bind(changeSelection);
$('pockets a').bind(changeSelection);

function changeSelection()
{
 var id = $(this).attr('id');
 var selectClass = 'img.select'+id;
 $("#selectPocket img").removeClass("visible"); 
 $(selectClass).addClass("visible"); 
}

Change your classes to match the case of the ids of the links, then do something like:

$('materials a').bind(changeSelection);
$('collars a').bind(changeSelection);
$('cuffs a').bind(changeSelection);
$('pockets a').bind(changeSelection);

function changeSelection()
{
 var id = $(this).attr('id');
 var selectClass = 'img.select'+id;
 $("#selectPocket img").removeClass("visible"); 
 $(selectClass).addClass("visible"); 
}
小ぇ时光︴ 2024-09-04 09:39:46
$("a").each(function() {
  $(this).click(function() {
    var src = $(this).children("img").attr("src");
    var img = $(".selectionimg").find("img[src$='"+src+"']");
    img.addClass("visible").siblings().removeClass("visible");
    return false;
  });
});

这假设所选图像与相应的可点击图像具有相同的 src。

$("a").each(function() {
  $(this).click(function() {
    var src = $(this).children("img").attr("src");
    var img = $(".selectionimg").find("img[src$='"+src+"']");
    img.addClass("visible").siblings().removeClass("visible");
    return false;
  });
});

This assumes, that the selected images are the same src as the corresponding clickable images.

坏尐絯℡ 2024-09-04 09:39:46

我还没有机会用图像来测试这一点,但您可以按如下方式缩短 HTML 和脚本:

$(document).ready(function(){
 /* names in the materials variable must match the image file name */
 /* the script will form the filename as follows: grey_shirt.png */
 var materials = {
  'shirt'  : ["grey", "red", "blue", "green"],
  'collar' : ["grey", "red", "blue", "green"],
  'cuff'   : ["grey", "red", "blue", "green"],
  'pocket' : ["grey", "red", "blue", "green"]
 }
 /* Set up Content */
 var i, c = '', s = '<div class="selectionimg">';
 $.each(materials, function(key, value){
  c += '<div class="' + key + '">';
  s += '<div class="select' + key + '">';
  for (i=0; i<value.length; i++) {
   c += '<a href="#" rel="' + value[i] + '"><img class="thumb" src="' + value[i] + '_' + key + '.png"></a>';
   s += '<img src="' + value[i] + '_' + key + '.png" height="250" width="250" class="large-img select' + value[i];
   s += (i==0) ? ' show" />' : ' hide" />'; // add show class only the first selection
  }
  c += '</div>';
  s += '</div>';
 })
 $('#content').html(c + s + '</div>');

 /* Set up scripting */
 $('#content a').click(function(){
  var type = $(this).parent().attr('class');
  var color = $(this).attr('rel');
  $('.select' + type).find('img').removeClass('visible').end()
   .find('img.select' + color).addClass('visible');
  return false;
 })
})

I haven't had a chance to test this with images, but you can shorten your HTML and script as follows:

$(document).ready(function(){
 /* names in the materials variable must match the image file name */
 /* the script will form the filename as follows: grey_shirt.png */
 var materials = {
  'shirt'  : ["grey", "red", "blue", "green"],
  'collar' : ["grey", "red", "blue", "green"],
  'cuff'   : ["grey", "red", "blue", "green"],
  'pocket' : ["grey", "red", "blue", "green"]
 }
 /* Set up Content */
 var i, c = '', s = '<div class="selectionimg">';
 $.each(materials, function(key, value){
  c += '<div class="' + key + '">';
  s += '<div class="select' + key + '">';
  for (i=0; i<value.length; i++) {
   c += '<a href="#" rel="' + value[i] + '"><img class="thumb" src="' + value[i] + '_' + key + '.png"></a>';
   s += '<img src="' + value[i] + '_' + key + '.png" height="250" width="250" class="large-img select' + value[i];
   s += (i==0) ? ' show" />' : ' hide" />'; // add show class only the first selection
  }
  c += '</div>';
  s += '</div>';
 })
 $('#content').html(c + s + '</div>');

 /* Set up scripting */
 $('#content a').click(function(){
  var type = $(this).parent().attr('class');
  var color = $(this).attr('rel');
  $('.select' + type).find('img').removeClass('visible').end()
   .find('img.select' + color).addClass('visible');
  return false;
 })
})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文