且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

当我点击li时为什么会触发ul?

更新时间:2022-02-02 01:29:41

那是因为事件冒出来了,你可以使用事件的 stopPropagation 方法对象:

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");  
});

如果要动态生成 li 元素您应该委派活动:

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');
})