jQuery: clicking nested elements

前端 未结 5 1780
粉色の甜心
粉色の甜心 2021-01-04 10:13

I have the following markup

  1. @qItem.CategoryText
相关标签:
5条回答
  • 2021-01-04 10:34

    Use .one(), as directed in the JQuery documentation.

    0 讨论(0)
  • 2021-01-04 10:43
    $(document).on("click", ".deleteIcon", function() {
      doActionB();
    });
    

    This method is the only one I found to work with nested elements, especially those generated by a library such as Pivottable (https://github.com/nicolaskruchten/pivottable)

    0 讨论(0)
  • 2021-01-04 10:55
    $(".deleteIcon").click(function(e){
        doActionB();
        e.stopPropagation();
    });
    
    0 讨论(0)
  • 2021-01-04 10:56

    You need to use event.stopPropagation() to prevents the event from bubbling up the DOM tree.

    $(".deleteIcon").click(function(event){
        event.stopPropagation()
        doActionA();    
    });
    

    The event you binded with delete icon is firing the parent event binding with ListItem, so you need to stop propagation for parent event when child is source of event.

    0 讨论(0)
  • 2021-01-04 10:56
    $(".deleteIcon").click(function(){
        doActionA();
        return false;  
    });
    
    0 讨论(0)
提交回复
热议问题