下拉子菜单无法识别点击

时间:2017-05-24 13:23:46

标签: javascript jquery html css

我已经获得了这个菜单,如果页面宽度无法容纳所有项目,那么剩余的项目会被添加到更多选项中。

问题是,当我点击more选项中的子菜单时,我无法打开链接。这是一个在点击时记录链接的示例。



$(".top_menu li").click(function() {

  console.log($(this).data('link'));

  // Checks if there is a link
  if (typeof $(this).data('link') !== 'undefined') {
    //document.location.href = $(this).data('link');
  }
});
$(".top_menu ul").each(function() {
  alignMenu(this);
  var robj = this;
  $(window).resize(function() {
    $(robj).append($($($(robj).children("li.hideshow")).children("ul")).html());
    $(robj).children("li.hideshow").remove();
    alignMenu(robj);
  });

  function alignMenu(obj) {
    var w = 0;
    var mw = $(obj).width() - 150;
    var i = -1;
    var menuhtml = '';
    jQuery.each($(obj).children(), function() {
      i++;
      w += $(this).outerWidth(true);
      if (mw < w) {
        menuhtml += $('<div>').append($(this).clone()).html();
        $(this).remove();
      }
    });
    $(obj).append('<li class="hideshow">MORE&nbsp;&nbsp;<i class="material-icons">keyboard_arrow_down</i><ul>' + menuhtml + '</ul></li>');
    $(obj).children("li.hideshow ul").css("top", $(obj).children("li.hideshow").outerHeight(true) + "px");

    // Opens the menu
    $(obj).children(".hideshow").click(function() {
      $(this).find("ul").animate({
        height: 'toggle'
      }, 'fast');
    });
  }
});
&#13;
.top_menu {
  width: 100%;
}

ul.horizontal-menu,
.horizontal-menu ul {
  list-style-type: none;
  margin: 0;
  padding: 0;
}

.horizontal-menu {
  float: left;
  width: 100%;
  background: #616161;
}

.horizontal-menu li {
  float: left;
  display: block;
  padding: 25px;
  color: #FFFFFF;
  text-decoration: none;
  -webkit-transition: border-color .218s;
  -moz-transition: border .218s;
  -o-transition: border-color .218s;
  transition: border-color .218s;
  background: #616161;
  cursor: pointer;
}

.horizontal-menu li .material-icons {
  margin: -10px;
}

.hideshow ul li {
  width: 250px;
  text-align: center;
}

.horizontal-menu li:hover {
  border-bottom: 3px solid rgb(246, 83, 20);
  padding-bottom: 22px;
  background: #484848;
}

.horizontal-menu li.hideshow ul {
  position: absolute;
  display: none;
  left: -203px;
  width: 300px;
}

.horizontal-menu li.hideshow {
  position: relative;
}

.hideshow ul {
  padding-bottom: 7px;
  background: #616161;
  border-radius: 0px 0px 4px 4px;
  margin-top: 25px;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!-- Material Icons (Google) -->
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<div class="top_menu">
  <ul class="horizontal-menu">
    <li>&nbsp;<i class="material-icons">search</i>&nbsp;</li>
    <li data-link="http://www.google.com">MENU 1</li>
    <li data-link="http://www.google.com">MENU 2</li>
    <li data-link="http://www.google.com">MENU 3</li>
    <li data-link="http://www.google.com">MENU 4</li>
    <li data-link="http://www.google.com">MENU 5</li>
    <li data-link="http://www.google.com">MENU 6</li>
    <li data-link="http://www.google.com">MENU 7</li>
    <li data-link="http://www.google.com">MENU 8</li>
    <li data-link="http://www.google.com">MENU 9</li>
    <li data-link="http://www.google.com">MENU 10</li>
    <li data-link="http://www.google.com">MENU 11</li>
    <li data-link="http://www.google.com">MENU 12</li>
    <li data-link="http://www.google.com">MENU 13</li>
    <li data-link="http://www.google.com">MENU 14</li>
  </ul>
</div>
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:3)

您需要进行事件委派,因为您的li正在动态添加

$(".top_menu li").click(function() {...

以上代码会直接向li添加事件监听器,这会在您动态重新排列DOM中的li时导致问题。

您必须使用事件委派。

$(".top_menu").on('click','li[data-link]',function() {...

此代码会向top-menu添加事件监听器,但会将事件委托给所有带有选择器li的死者li[data-link]

了解Event Delegation

  

事件委托允许我们将单个事件监听器附加到   父元素,将为匹配a的所有后代触发   选择器,无论这些后代现在存在还是被添加到   将来

查看我的answer有关事件委派的说明

<强>段

$(".top_menu").on('click','li[data-link]',function() {

  console.log($(this).data('link'));

  // Checks if there is a link
  if (typeof $(this).data('link') !== 'undefined') {
    //document.location.href = $(this).data('link');
  }
});
$(".top_menu ul").each(function() {
  alignMenu(this);
  var robj = this;
  $(window).resize(function() {
    $(robj).append($($($(robj).children("li.hideshow")).children("ul")).html());
    $(robj).children("li.hideshow").remove();
    alignMenu(robj);
  });

  function alignMenu(obj) {
    var w = 0;
    var mw = $(obj).width() - 150;
    var i = -1;
    var menuhtml = '';
    jQuery.each($(obj).children(), function() {
      i++;
      w += $(this).outerWidth(true);
      if (mw < w) {
        menuhtml += $('<div>').append($(this).clone()).html();
        $(this).remove();
      }
    });
    $(obj).append('<li class="hideshow">MORE&nbsp;&nbsp;<i class="material-icons">keyboard_arrow_down</i><ul>' + menuhtml + '</ul></li>');
    $(obj).children("li.hideshow ul").css("top", $(obj).children("li.hideshow").outerHeight(true) + "px");

    // Opens the menu
    $(obj).children(".hideshow").click(function() {
      $(this).find("ul").animate({
        height: 'toggle'
      }, 'fast');
    });
  }
});
.top_menu {
  width: 100%;
}

ul.horizontal-menu,
.horizontal-menu ul {
  list-style-type: none;
  margin: 0;
  padding: 0;
}

.horizontal-menu {
  float: left;
  width: 100%;
  background: #616161;
}

.horizontal-menu li {
  float: left;
  display: block;
  padding: 25px;
  color: #FFFFFF;
  text-decoration: none;
  -webkit-transition: border-color .218s;
  -moz-transition: border .218s;
  -o-transition: border-color .218s;
  transition: border-color .218s;
  background: #616161;
  cursor: pointer;
}

.horizontal-menu li .material-icons {
  margin: -10px;
}

.hideshow ul li {
  width: 250px;
  text-align: center;
}

.horizontal-menu li:hover {
  border-bottom: 3px solid rgb(246, 83, 20);
  padding-bottom: 22px;
  background: #484848;
}

.horizontal-menu li.hideshow ul {
  position: absolute;
  display: none;
  left: -203px;
  width: 300px;
}

.horizontal-menu li.hideshow {
  position: relative;
}

.hideshow ul {
  padding-bottom: 7px;
  background: #616161;
  border-radius: 0px 0px 4px 4px;
  margin-top: 25px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!-- Material Icons (Google) -->
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<div class="top_menu">
  <ul class="horizontal-menu">
    <li>&nbsp;<i class="material-icons">search</i>&nbsp;</li>
    <li data-link="http://www.google.com">MENU 1</li>
    <li data-link="http://www.google.com">MENU 2</li>
    <li data-link="http://www.google.com">MENU 3</li>
    <li data-link="http://www.google.com">MENU 4</li>
    <li data-link="http://www.google.com">MENU 5</li>
    <li data-link="http://www.google.com">MENU 6</li>
    <li data-link="http://www.google.com">MENU 7</li>
    <li data-link="http://www.google.com">MENU 8</li>
    <li data-link="http://www.google.com">MENU 9</li>
    <li data-link="http://www.google.com">MENU 10</li>
    <li data-link="http://www.google.com">MENU 11</li>
    <li data-link="http://www.google.com">MENU 12</li>
    <li data-link="http://www.google.com">MENU 13</li>
    <li data-link="http://www.google.com">MENU 14</li>
  </ul>
</div>

答案 1 :(得分:0)

问题是$(".top_menu li").click(...)函数仅适用于当时存在的li元素,并且您的代码正在删除并稍后将它们重新附加到dom。为了使用当时或以后存在的链接附加到任何li元素,您可以使用.on('click', selector, ...)。为避免包含li.hideshow元素,您可以使用li[data-link]作为选择器。

&#13;
&#13;
$(".top_menu").on('click', 'li[data-link]', function() {

  console.log($(this).data('link'));

  // Checks if there is a link
  if (typeof $(this).data('link') !== 'undefined') {
    //document.location.href = $(this).data('link');
  }
});
$(".top_menu ul").each(function() {
  alignMenu(this);
  var robj = this;
  $(window).resize(function() {
    $(robj).append($($($(robj).children("li.hideshow")).children("ul")).html());
    $(robj).children("li.hideshow").remove();
    alignMenu(robj);
  });

  function alignMenu(obj) {
    var w = 0;
    var mw = $(obj).width() - 150;
    var i = -1;
    var menuhtml = '';
    jQuery.each($(obj).children(), function() {
      i++;
      w += $(this).outerWidth(true);
      if (mw < w) {
        menuhtml += $('<div>').append($(this).clone()).html();
        $(this).remove();
      }
    });
    $(obj).append('<li class="hideshow">MORE&nbsp;&nbsp;<i class="material-icons">keyboard_arrow_down</i><ul>' + menuhtml + '</ul></li>');
    $(obj).children("li.hideshow ul").css("top", $(obj).children("li.hideshow").outerHeight(true) + "px");

    // Opens the menu
    $(obj).children(".hideshow").click(function() {
      $(this).find("ul").animate({
        height: 'toggle'
      }, 'fast');
    });
  }
});
&#13;
.top_menu {
  width: 100%;
}

ul.horizontal-menu,
.horizontal-menu ul {
  list-style-type: none;
  margin: 0;
  padding: 0;
}

.horizontal-menu {
  float: left;
  width: 100%;
  background: #616161;
}

.horizontal-menu li {
  float: left;
  display: block;
  padding: 25px;
  color: #FFFFFF;
  text-decoration: none;
  -webkit-transition: border-color .218s;
  -moz-transition: border .218s;
  -o-transition: border-color .218s;
  transition: border-color .218s;
  background: #616161;
  cursor: pointer;
}

.horizontal-menu li .material-icons {
  margin: -10px;
}

.hideshow ul li {
  width: 250px;
  text-align: center;
}

.horizontal-menu li:hover {
  border-bottom: 3px solid rgb(246, 83, 20);
  padding-bottom: 22px;
  background: #484848;
}

.horizontal-menu li.hideshow ul {
  position: absolute;
  display: none;
  left: -203px;
  width: 300px;
}

.horizontal-menu li.hideshow {
  position: relative;
}

.hideshow ul {
  padding-bottom: 7px;
  background: #616161;
  border-radius: 0px 0px 4px 4px;
  margin-top: 25px;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!-- Material Icons (Google) -->
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<div class="top_menu">
  <ul class="horizontal-menu">
    <li>&nbsp;<i class="material-icons">search</i>&nbsp;</li>
    <li data-link="http://www.google.com">MENU 1</li>
    <li data-link="http://www.google.com">MENU 2</li>
    <li data-link="http://www.google.com">MENU 3</li>
    <li data-link="http://www.google.com">MENU 4</li>
    <li data-link="http://www.google.com">MENU 5</li>
    <li data-link="http://www.google.com">MENU 6</li>
    <li data-link="http://www.google.com">MENU 7</li>
    <li data-link="http://www.google.com">MENU 8</li>
    <li data-link="http://www.google.com">MENU 9</li>
    <li data-link="http://www.google.com">MENU 10</li>
    <li data-link="http://www.google.com">MENU 11</li>
    <li data-link="http://www.google.com">MENU 12</li>
    <li data-link="http://www.google.com">MENU 13</li>
    <li data-link="http://www.google.com">MENU 14</li>
  </ul>
</div>
&#13;
&#13;
&#13;

答案 2 :(得分:-1)

您的事件触发器不正确。您可以将其更改为以下内容或将类添加到其他ul。

<p:column selectionMode="multiple" exportable="false" style="text-
align:center;">
</p:column>
  <p:ajax event="rowSelectCheckbox" update="deleteButton"/>
  <p:ajax event="rowUnselectCheckbox" update="deleteButton"/>