简体   繁体   English

一个元素内的多次点击事件

[英]Multiple click events inside one element

I have the following HMTL code: 我有以下HMTL代码:

<li>
    <img .../>
    <img .../>
    <img .../>
</li>

I want to register different functions for each element (one for the list item, and three other for the images). 我想为每个元素注册不同的功能(一个用于列表项,另一个用于图像)。 I registered click events on all but when I click the images, the li function triggers. 我注册了所有单击事件,但单击图像时会触发li函数。

li.addEventListener("click",open,false);
img1.addEventListener("click",edit,false);
img2.addEventListener("click",delete,false);
img3.addEventListener("click",add,false);

I googled what happens when you set to true the last parameter but still no luck. 我用谷歌搜索当您将最后一个参数设置为true但仍然没有运气时会发生什么。

Thanks for all the help. 感谢您的所有帮助。

When you define your functions, stop event from propagating like so: 定义函数时,请停止事件传播,如下所示:

function open(e) {
   e.stopPropagation();
}

function edit(e) {
   e.stopPropagation()
}

and so on... 等等...

expanding on designcise's answer... you need to use e.stopPropagation() to stop the event from bubbling up 扩展designcise的答案...您需要使用e.stopPropagation()来阻止事件冒泡

$('li img').on('click',function(e){
    e.stopPropagation();
    alert('img clicked');
});

here a jsfiddle http://jsfiddle.net/hbGRS/ 这是一个jsfiddle http://jsfiddle.net/hbGRS/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM