简体   繁体   English

使用jQuery更改单击的div内的锚标记的css

[英]change the css of anchor tag inside clicked div using jquery

I have multiple div with classes 我的课程有多个div

.product_wishlist .product_wishlist

I want to change the color of the particular anchor tag on which the user click inside this div, but now all the anchor tag colors which are inside .pw div are changed. 我想更改用户在该div中单击的特定锚标签的颜色,但是现在.pw div内的所有锚标签颜色都已更改。 Is it possible to add selected css to the clicked element. 是否可以将选定的CSS添加到clicked元素中。 Please help to sort out my issue. 请帮助解决我的问题。

 $(document).ready(function() { $(document).on('click','.pw',function(e){ $('.pw > a').css('background-color', 'green'); }); }); 
 .product_wishlist > a { background-color: #e91e63; height: 30px; position: absolute; right: -32px; text-align: center; top: 15px; width: 30px; -webkit-transition-duration: 500ms; transition-duration: 500ms; z-index: 9; } .wishlish-selected > a { background-color: black; height: 30px; position: absolute; right: -32px; text-align: center; top: 15px; width: 30px; -webkit-transition-duration: 500ms; transition-duration: 500ms; z-index: 9; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> <div class="product_wishlist pw"> <a href="#" target="_blank"><i class="ti-heart"></i>Wishlist</a> </div> 

Use this to refer the current element and .find() the anchor element from that: 使用this来引用当前元素,并从该元素.find() 元素:

Change 更改

$('.pw > a').css('background-color', 'green');

To

$(this).find('a').css('background-color', 'green');

 $(document).ready(function() { $(document).on('click','.pw',function(e){ $(this).find('a').addClass('wishlish-selected') }); }); 
 .wishlish-selected{ background-color: green; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> <div class="product_wishlist pw"> <a href="#" target="_blank"><i class="ti-heart"></i>Wishlist 1</a> </div> <div class="product_wishlist pw"> <a href="#" target="_blank"><i class="ti-heart"></i>Wishlist 2</a> </div> <div class="product_wishlist pw"> <a href="#" target="_blank"><i class="ti-heart"></i>Wishlist 3</a> </div> 

As you already specifying the anchor element after the class in the CSS selector, you do not need to find anchor element again in jQuery, simply add the class to the current element. 由于您已经在CSS选择器中的类之后指定了元素,因此无需在jQuery中再次找到元素,只需将类添加到当前元素即可。 Also, there is an issue with position: absolute , to show the output I ignored that property here: 另外,还有一个position: absolute问题position: absolute ,为了显示输出,我在这里忽略了该属性:

 $(document).ready(function() { $(document).on('click','.pw',function(e){ $(this).addClass('wishlish-selected') }); }); 
 .product_wishlist > a { background-color: #e91e63; height: 30px; right: -32px; text-align: center; top: 15px; width: 30px; -webkit-transition-duration: 500ms; transition-duration: 500ms; z-index: 9; } .wishlish-selected > a { background-color: black; height: 30px; right: -32px; text-align: center; top: 15px; width: 30px; -webkit-transition-duration: 500ms; transition-duration: 500ms; z-index: 9; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> <div class="product_wishlist pw"> <a href="#" target="_blank"><i class="ti-heart"></i>Wishlist 1</a> </div> <div class="product_wishlist pw"> <a href="#" target="_blank"><i class="ti-heart"></i>Wishlist 2</a> </div> <div class="product_wishlist pw"> <a href="#" target="_blank"><i class="ti-heart"></i>Wishlist 3</a> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM