jQuery委托事件目标与子项冲突

时间:2016-11-23 13:03:22

标签: javascript jquery event-handling

当我点击menu-link专门针对span时,有效的课程没有添加或切换,如果我点击不在跨度上,那么一切正常。

$(document).on("click touchstart", ".menu-link", function(event) {
  $("body").toggleClass('nav-open');
  utilities.toggleContent(event);
});

utilities = function() {
  var toggleContent = function(event) {
      //If clicked element is an anchor and has a hash target, use it to select the toggle DIV
      if (event.target.hash) {
        //If it's one of the menu links
        if ($(event.target).hasClass('menu-link')) {

          //Close any open menus which aren't the target menu
          $(".menu-content.active").not($(event.target.hash)).removeClass('active');
        }

        //Toggle the target menu
        $(event.target.hash).toggleClass('active');
        event.preventDefault();
      } else {
        $(this).next().toggleClass('active');
        $(this).toggleClass('active');
        return false;
      }
    },
    // ...
<a href="#mainMenu" class="menu-link">
  <span class="iconbar"></span>
  <span class="iconbar"></span>
  <span class="iconbar"></span>
</a>

0 个答案:

没有答案