[英]Image borders change onclick
I have 5 images in the same div id="palettes", I want all images to have 3px solid grey border, the one clicked change to black. 我在同一个div id =“ palettes”中有5张图像,我希望所有图像都具有3px的纯灰色边框,其中一个单击更改为黑色。
The code works, but when I click any of this 5 images inside the div the javascript add 3px solid grey to all images in the page not only in the div I want. 该代码有效,但是当我单击div内的这5张图像中的任何一张时,javascript不仅向我想要的div中的页面中的所有图像添加3px纯灰色。 How do I specify this in the code?
如何在代码中指定?
$('#palettes img').click(function () { $('img').not($(this)).css({'border':'3px solid grey'}); $(this).css({'border':'3px solid black'}); });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="palettes"> <a href="javascript:;"><img style="border: 3px solid grey;" src="palette1.jpg"/></a> <a href="javascript:;"><img style="border: 3px solid grey;" src="palette2.jpg"/></a> <a href="javascript:;"><img style="border: 3px solid grey;" src="palette3.jpg"/></a> <a href="javascript:;"><img style="border: 3px solid grey;" src="palette4.jpg"/></a> <a href="javascript:;"><img style="border: 3px solid grey;" src="palette5.jpg"/></a> </div>
Bah, I just fixed it. ah,我刚修好。 Sorry.
抱歉。
$('#palettes img').click(function () {
$('#palettes img').not($(this)).css({'border':'3px solid grey'});
$(this).css({'border':'3px solid black'});
});
You can use children
element to access to all children of an element: 您可以使用
children
元素来访问该元素的所有子元素:
$('#palettes img').click(function () {
$( '#palettes' ).children( 'img' ).css( {'border':'3px solid grey'} );
});
<div id="palettes">
<a href="javascript:;"><img style="border: 3px solid grey;" src="palette1.jpg"/></a>
<a href="javascript:;"><img style="border: 3px solid grey;" src="palette2.jpg"/></a>
<a href="javascript:;"><img style="border: 3px solid grey;" src="palette3.jpg"/></a>
<a href="javascript:;"><img style="border: 3px solid grey;" src="palette4.jpg"/></a>
<a href="javascript:;"><img style="border: 3px solid grey;" src="palette5.jpg"/></a>
</div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.