JQuery live () and stopPropagation () problem

I know that the live () event handles events that differ in all other jQuery events. jQuery recommends using 'return false', but in my case this does not work.

Problem:

I have a DIV that contains an anchor tag.

The div is related to using live (). Whenever I click on the anchor tag inside this DIV, it bubbles up and triggers a DIV event. If I attach the event to a tag A that returns false, this will prevent the link from opening. In this case, neither stopPropagation () nor return false work. Are there any other options? Ideally, I would like to save the live () event.

+3
source share
2 answers

element.preventDefault();

stopPropagation , preventDefault .

+5

, href URL-, , , javascript - .

$("#the-div").live('click', function (e) {
  var tag = e.target.tagName;
  if (tag === "A") {
    window.location.href = e.target.href;
    return false;
  }
  // else do the div click handling
});

, , , , .

+2

Source: https://habr.com/ru/post/1742137/


All Articles