gpt4 book ai didi

javascript - 下拉子菜单无法识别点击

转载 作者:行者123 更新时间:2023-11-30 15:18:18 25 4
gpt4 key购买 nike

我有这个菜单,如果页面宽度不能容纳它的所有元素,那些剩余的元素将被添加到更多选项。

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

$(".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');
});
}
});
.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>

最佳答案

您需要进行事件委托(delegate),因为您的 li 正在动态添加

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

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

你必须使用事件委托(delegate)。

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

此代码将事件监听器添加到 top-menu 但将事件委托(delegate)给其所有带有选择器 li[data-link] 的后代 li >

了解 Event Delegation

Event delegation allows us to attach a single event listener, to a parent element, that will fire for all descendants matching a selector, whether those descendants exist now or are added in the future.

查看我的 answer用于解释事件委托(delegate).

片段

$(".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>

关于javascript - 下拉子菜单无法识别点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44159688/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com