简体   繁体   English

仅当未点击内部标签时才允许周围div的click事件触发

[英]Allow surrounding div's click event only to trigger when inner tags are not clicked

I've prepared a fiddle here: 我在这里准备了一个小提琴:

https://jsfiddle.net/9m4bgyq8/16/ https://jsfiddle.net/9m4bgyq8/16/

As you can see, I am trying to surround a checkbox + label with a div, and apply a click-event to the div, which is only supposed to trigger when the input or label are NOT being clicked. 如您所见,我试图用div包围复选框+标签,并将click事件应用于div,该事件仅应在未单击输入或标签时触发。 I even applied a fakeClick-function here, which basically does nothing, and tried to suppress bubbling, however it doesn't help. 我什至在这里应用了一个fakeClick函数,该函数基本上什么都不做,并试图抑制冒泡,但是它没有帮助。

It's executing the divs function no matter what I click. 无论我单击什么,它都在执行divs功能。

For everyone who wants to see crucial code here: 对于每个想要在此处查看关键代码的人:

<ul>
  <div data-bind="click: clickFunction">
    <input id="muteCheckBox" type="checkbox" data-bind="click: fakeClick, clickbubble: false"/>
    <label for="muteCheckBox" data-bind="click: fakeClick, clickbubble: false">I'm a label</label>
  </div>
</ul>

and

var functionWrapper= {
    clickFunction : function(data, event) {
        window.alert("Event triggered");
    },
    fakeClick : function(data,event) {
        //Do nothing
    }
};

ko.applyBindings(functionWrapper);

It's not obvious to see here due to CSS being missing, but basically my div contains an input and label tag, and I want only the inputs and labels tag function to execute when a click is placed there, not the divs function. 由于缺少CSS,因此在这里看不见,但基本上我的div包含输入和标签标记,并且我只希望在单击时执行输入和标签标记功能,而不希望divs功能。

Right now, the inputs and labels function are not executed at all. 现在,根本不执行输入和标签功能。

Thanks in advance! 提前致谢!

According to your fiddle the problem is a mispelled keyword. 根据您的小提琴,问题是关键字拼写错误。 Look at the fiddle here and notice the "clickBubble" instead of the "clickbubble". 查看此处的小提琴,注意“ clickBubble”而不是“ clickbubble”。

Another way to do that could also be calling the function stopPropagation on the event inside the fakeClick function: 做到这一点的另一种方法也可以在fakeClick函数内的事件上调用函数stopPropagation:

fakeClick : function(data,event)
{
    event.stopPropagation();
}

Your modified fiddle here 在这里修改过的小提琴

For more info about the stopPropagation look here 有关stopPropagation的更多信息,请单击此处

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

相关问题 我有div复选框,当我单击它时需要它得到div的内部文本,我只单击它 - i have div's with check box i need when i click on it get the inner text of div i clicked on it only 如何仅在单击子项时触发父项单击事件 - How to ONLY trigger parent click event when a child is clicked 如果单击内部div,如何防止来自外部div的click事件 - How to prevent click event from outer div if inner div is clicked 当孩子被点击时触发父点击事件 - trigger parent click event when child is clicked jQuery-div中的div,如何在单击内部时仅运行1 click事件 - jQuery - div inside div, how to run only 1 click event when inside has been clicked 如果单击其中的链接,请勿触发div click事件 - Do not trigger div click event if a link inside of it is clicked Firefox没有在周围的div上注册click事件 - Firefox not registering click event on surrounding div 如何创建一个单击事件,其中我有多个 div,并且每个 div 都有一个具有相同 Id 的按钮,但单击时仅影响父 div,如何? - How can I create a click event where I have several div's and each having a button with the same Id but when clicked only affect the parent div, how? 防止在单击内部div时触发单击处理程序 - Prevent a click handler from firing when an inner div is clicked 骨干点击事件:仅在实际点击的元素上触发 - Backbone click event: Trigger only on element actually clicked on
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM