gpt4 book ai didi

jquery - 如何处理嵌套列表的内部 LI 单击

转载 作者:太空宇宙 更新时间:2023-11-03 23:14:28 25 4
gpt4 key购买 nike

fiddle :http://jsfiddle.net/34ng9sto/14/

JQuery:

$(function () {
$(".uSPInner").hide();

//modify the "hover" to "click" and merge with thesecond "click" function. SlideToggle only if a child UL exist within the parent UL
$(".clickMe").closest("li").hover(function () {
$(this).closest("li").find("ul").slideDown();
}, function () {
$(this).closest("li").find("ul").slideUp();
});
$('.clickMe').click(function () {
alert("outer clicked");
$('.clickMe').removeAttr('id');
$(this).attr('id', 'current');
$('.dispArtBody').addClass('hideContent');
var element = $(this).attr("data-toggle");
$(element).removeClass('hideContent');
});

$('.uSPInner').click(function () { //if the inner LI is clicked, set the inner clicked LI ID to "current" and also the parent LI ID to "current"
alert("inner clicked");
});
$('.uSPStyle li a').filter(function () {
return $.trim($(this).html()) == '';
}).parent().hide();
$('.uSPInner li a').filter(function () {
return $.trim($(this).html()) == '';
}).parent().hide();
});

我想做的是

  • 不是悬停,而是展开子 UL,通过单击和合并来处理它第二次点击事件。
  • 单击子 UL LI 时,将其设置为“当前”,以及被点击的子 UL LI 的父级。

最佳答案

可能有更好的解决方案,但目前这可以帮助您:

$('.clickMe').click(function () {

var $this = $(this);

$this.closest("li").find("ul").slideToggle();

$('.clickMe').removeClass('current');

$this.addClass('current');
$this.parents('.uSPInner').siblings('.clickMe').addClass('current');

});

不要使用悬停函数的进出处理程序,只需在点击事件中使用 slideToggle() 函数即可。

注意:我将 ID current 更改为一个类,因为当您要将它应用于多个元素时,必须记住 ID 在文档中必须是唯一的。

Demo


补充:

我建议将类 uSPInner 设置为

.uSPInner{
display: none;
}

而不是在页面加载时通过 jQuery 隐藏它。

关于jquery - 如何处理嵌套列表的内部 LI 单击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31185922/

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