Why ul gets triggered when I click on li?

 ̄綄美尐妖づ 提交于 2019-12-06 01:46:49

That's because events bubble up, you can use stopPropagation method of the event object:

$(".contry-list").on("click", function(event){
     event.stopPropagation();
     alert("clicked on country's list");  
});

If you are generating the li elements dynamically you should delegate the event:

$(document).on('click', '.coutry-list', function(){
   console.log('li element is clicked');
})
$(".contry-list").on("click", function(event){
     event.stopPropagation();
     alert("clicked on country's list");
});

Need to use event.stopPropagation()

$(".contry-list").on("click", function(e){
     e.stopPropagation();
     alert("clicked on country's list");
});

Demo Here

A fix in jQuery would be:

$(".contry-list").on("click", function(event){
     alert("clicked on country's list");  // not working!
    return false; 
});

But as epascarello points out, you should learn about DOM event bubbling (and capturing) to understand it.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!