简体   繁体   中英

How to set $(this) as the clicked element

In the following code:

// submit an item
$(document).on("click", ".match-item", function(event) {


    // how to set $(this) == $('.match-item') clicked?

});

I'm looking to retrieve $(this) as the clicked item and not the document itself. How would I do this?

This is more of clarification rather than answer.

this is already referring to the currently clicked element.

 $(document).on("click", ".match-item", function(event) { console.log($(this).attr('class')); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="parent"> Parent <div class="match-item">----Click Me</div> </div> 

How about

$('.match-item').click(function() {
     //your code with $(this) here
});

I am pretty sure $(this) will refer to the element with class match-item

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