jquery stopPropagation problem with live method

前端 未结 3 1270
灰色年华
灰色年华 2021-01-13 10:57

Jquery stopPropagation method dosen\'t work with live method. Below the code is works fine with click instead of live method. Any help greatly appreciated.

Code:<

3条回答
  •  执笔经年
    2021-01-13 11:19

    I had no problems using pure javascript for the same. Though this doesn't relate to OP's question(looks complicated) just posting this answer for others who prefers JS over jQuery

    HTML:

    jQuery:

    $(".ac_results li").live('click', function() {
       alert('li called')
    });
    
    $(".ac_results li input[type='checkbox']").live('click', function(e) {        
       e.stopPropagation();            //does not work
       alert('check box called');
    });
    

    Javascript:

    //get child elements
    var checkbox = document.getElementsByTagName('input');
    for(var i=0;i

    var checkbox = document.getElementsByTagName('input');
    for (var i = 0; i < checkbox.length; i++) {
      checkbox[i].onclick = callCheck;
    }
    var li = document.getElementsByTagName('li');
    for (var j = 0; j < li.length; j++) {
      li[j].onclick = liClick;
    }
    
    function liClick(e) {
      alert('list item clicked');
    }
    
    function callCheck(e) {
      e.stopPropagation();
      alert('checkbox clicked');
    }

提交回复
热议问题