In Jquery how do you find out the 'eq' of an element of what is being clicked?

前端 未结 4 1617
无人共我
无人共我 2021-02-05 01:49

When one of the divs inside \'uploadChoice\' is clicked, how can I ascertain which one is clicked? Can I return an \'eq\' value somehow?

 
相关标签:
4条回答
  • 2021-02-05 02:26

    You can get the contents of the div with

    $("#uploadChoice div").click(function(){
        var choice = $(this).html;
    }
    

    And then just do a switch statement or some nested ifs.

    Personally though, I'd add an ID to each of the divs and just check the ID of the clicked element.

    0 讨论(0)
  • 2021-02-05 02:29

    You can add selectors as well, and pass "$(this)" as argument:

    $(document).on('click', 'button.testbutton', function () {
      
      var index = $('div.example > div.xyz').find('button.testbutton').index($(this));
    
    });

    0 讨论(0)
  • 2021-02-05 02:32
    $('#uploadChoice div').click(function(event) {
      var index = $(this).index();
    });
    
    0 讨论(0)
  • 2021-02-05 02:42

    An easier alternative that does not require duplicating a selector is the following

    $('#uploadChoice div').click(function(event) {
      var index = $(this).prevAll().length;
    });
    
    0 讨论(0)
提交回复
热议问题