gpt4 book ai didi

javascript - jquery 切换附加删除

转载 作者:行者123 更新时间:2023-11-30 09:40:31 24 4
gpt4 key购买 nike

这是一个带有子菜单的菜单。当我点击子菜单项时,我需要在菜单项“Everyday”旁边切换子菜单文本。像下图一样enter image description here

(function($) {
$(".menu .sub-menu li a").each(function() {
var dayName = $(this).text();
$(this).on("click", function() {
$(this).closest(".menu").children("li").children("a").append('<span class="day-name">' + dayName + '</span>');
});
});
})(jQuery);
ul {
margin: 0;
padding: 0;
list-style: none;
}

a {
text-decoration: none;
}

.menu > li {
position: relative;
}

.menu > li > a {
background-color: #eee;
color: #333;
display: inline-block;
padding: 10px 20px;
}

.menu li:hover > .sub-menu {
opacity: 1;
visibility: visible;
}

.menu .sub-menu {
position: absolute;
left: 0;
background-color: #fff;
box-shadow: 0px 2px 2px 1px rgba(0, 0, 0, 0.2);
min-width: 200px;
opacity: 0;
visibility: hidden;
transition: all 0.3s ease 0s;
}

.menu .sub-menu li {
}

.menu .sub-menu li a {
color: #777;
display: block;
padding: 5px 10px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<ul class="menu">
<li>
<a href="#">Everyday</a>
<ul class="sub-menu">
<li><a href="#">Sat</a></li>
<li><a href="#">Sun</a></li>
<li><a href="#">Mon</a></li>
<li><a href="#">Tue</a></li>
<li><a href="#">Wed</a></li>
<li><a href="#">Thu</a></li>
<li><a href="#">Fri</a></li>
</ul>
</li>
</ul>

这就是我正在尝试的方法,但它不起作用。

最佳答案

您可以使用 Javascript 的 join & jQuery 的 - hasClass & toggleClass使用 on('click') 的方法像这样的事件:

参见 jsFiddle

或查看下面的代码片段:

$(function() {

$('.menu .sub-menu li a').on('click', function(e) {

$(this).toggleClass('selected');
var txt = $('#title').text();
var total_len = $('.menu .sub-menu li').length;
var count = 0;
var values = [];

$('.menu .sub-menu li a.selected').each(function(i) {
values.push($(this).text());
count++;
});

if(count == total_len) {
txt = "Every Day";
} else {
txt = "Every " + values.join(',');
}
$('#title').text(txt);

});

})
ul {
margin: 0;
padding: 0;
list-style: none;
}

a {
text-decoration: none;
}

.menu > li {
position: relative;
}

.menu > li > a {
background-color: #eee;
color: #333;
display: inline-block;
padding: 10px 20px;
}

.menu li:hover > .sub-menu {
opacity: 1;
visibility: visible;
}

.menu .sub-menu {
position: absolute;
left: 0;
background-color: #fff;
box-shadow: 0px 2px 2px 1px rgba(0, 0, 0, 0.2);
min-width: 200px;
opacity: 0;
visibility: hidden;
transition: all 0.3s ease 0s;
}

.menu .sub-menu li {
}

.menu .sub-menu li a {
color: #777;
display: block;
padding: 5px 10px;
}

.menu .sub-menu li a.selected {
color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<ul class="menu">
<li>
<a id="title" href="#">Every </a>
<ul class="sub-menu">
<li><a href="#">Sat</a></li>
<li><a href="#">Sun</a></li>
<li><a href="#">Mon</a></li>
<li><a href="#">Tue</a></li>
<li><a href="#">Wed</a></li>
<li><a href="#">Thu</a></li>
<li><a href="#">Fri</a></li>
</ul>
</li>
</ul>

关于javascript - jquery 切换附加删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41348233/

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