最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Knockout JS prevent click event triggering on a specific child - Stack Overflow

matteradmin5PV0评论

I've seen similar questions asked but I couldn't make it work for me with Knockout 'data-bind'

I have a parent div with some multiple divs inside. On the parent div I have a click event, that I do not want to fire on a specific child div that has some other event handling.

I have something like this:

<div class='parent' dataBind='click: parentClicked'>
  <div class='child' dataBind='click: childClicked'></div>
  ....some other stuff...
</div>


In my view model I tried:

function parentClicked() {
  alert('parent clicked');
}
function childClicked(event) {
  alert('child clicked');
  event.stopPropagation();
}


I get that 'event.stopPropagation()' is not a function.
What am I doing wrong?

I've seen similar questions asked but I couldn't make it work for me with Knockout 'data-bind'

I have a parent div with some multiple divs inside. On the parent div I have a click event, that I do not want to fire on a specific child div that has some other event handling.

I have something like this:

<div class='parent' dataBind='click: parentClicked'>
  <div class='child' dataBind='click: childClicked'></div>
  ....some other stuff...
</div>


In my view model I tried:

function parentClicked() {
  alert('parent clicked');
}
function childClicked(event) {
  alert('child clicked');
  event.stopPropagation();
}


I get that 'event.stopPropagation()' is not a function.
What am I doing wrong?

Share Improve this question asked Jan 15, 2016 at 9:28 DragosDragos 77610 silver badges33 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

You have to use clickBubble: false. For reference: Preventing the event from bubbling

<div class='parent' dataBind='click: parentClicked'>
  <div class='child' dataBind='click: childClicked, clickBubble: false'></div>
  ....some other stuff...
</div>
Post a comment

comment list (0)

  1. No comments so far