简体   繁体   中英

How to get only clicked element by class name?

I have some elements with the same class name but I want to get only clicked element class and change it.

    var icon = $('.opener i'); // I want to take this class of only clicked element
   

Function

      $('.cate-inner span.opener').on("click", function(){
          var icon = $('.opener i');
        if ($(this).hasClass("plus")) {
          $(this).parent().find('.mega-sub-menu').slideDown();
          $(this).removeClass('plus');
            $(this).addClass('minus');
            icon.removeClass('fa-plus');
            icon.addClass('fa-minus')
        }
});

Try to use context in query

var icon = $('i', $(this));

For better understanding you can check this docs

Without html it's hard to tell but i will guess than replacing

var icon = $('.opener i');

by

var icon = $(this).find('i');

can do the trick

"li").click(function() {
   var colorClass = this.className;
   console.lo

Try to do just like this.

Can you post your Html where the icon located?

Basically, you can locate the icon with $(this).closest( ".opener i" )

$(this).closest('.parentElemnt').find('.opener i')

But to give you the exact code I need to see the HTML code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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