2010-04-06 64 views
1

我想单击一个触发器并显示特定图像。有多个触发器可以在一个集合中显示与其相关的特定图像。有4套当我点击其中一个触发器时,我的挑战是切换其他图像以仅隐藏在这个“设置”中,因为每组中只能有一个图像显示。使用jquery更改多个对象的新类名称

这是迄今为止我已经把下面的HTML:

<!-- 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 class="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 class="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 class="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 class="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> 

的jQuery如何可以用来改变图像的一类“秀”,并确保在同一格的所有其他图像设置为“隐藏”类?

回答

0

有一个用于jquery的toggle()方法。但在我去看之前,你可以这样做这样一个长手:

首先,你的div类不是我能说的类,而是id,因为它们对于这个集合是唯一的。所以你应该让

<div id="cuffs" class="set"> 

然后,你可以用jQuery做到这一点:

$(".set a").click(function() { 
    $(this).parent("img").removeClass("visible"); 
    $("img",this).addClass("visible"); 
}); 

就像我说的,有可能是与toggle()的巧妙方式,但基本上,外选择有onclick处理程序。该函数的第一行表示选择位于外部div(父级)内部的所有图像并删除“可见”类。第二行说要将“可见”类添加到点击链接内部的图像中。

这样的,而不是一个“隐藏”类和“看得见的”类,你可以有内部的“套”的所有图像DIV有一个默认的CSS规则:

.sets img [ 
     display: none; 
} 

,然后有对于可见光图像另一个CSS规则:

.sets img.visible { 
     display: block; 
} 

因此而不必担心触发的,你只是去除所有的图像在类的DIV(无论他们是否有和无的),然后重新添加它到需要它的人。

+0

你在正确的轨道上,我需要然而,被点击的图像显示/隐藏是在一个完全不同的div,然后实际显示/隐藏发生,而不一定是父div。基本上我需要能够从位于页面任何位置的div中的拇指触发此事件。 再次感谢您的帮助。我觉得我快到了。 – liquilife 2010-04-06 17:56:42