且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何获得数组中每个项目的最后一个元素子项,然后在单击它们时显示它们

更新时间:2023-11-07 12:59:10

因此,您的问题是imo,您可能不应该这样决定html中的click函数.

So your problem is, imo, that you probably shouldn't be deciding the click function in the html like that.

在我看来,该网站上已经安装了jQuery,那么为什么不使用它来简化事情呢?您想为<i>元素分配一个点击,如下所示:

It looks to me like the site already has jQuery installed on it, so why don't you use that to make things simpler? You want to assign a click to the <i> elements like this:

$('#ib-menu i').click(function() { /* do your work in here*/ });

在函数内部,this是当前单击的元素,而$(this)是该元素的jQuery选择版本.您可以使用jQuery添加和删除类,添加和删除样式,以及查找作为所选元素的父元素或子元素的元素.

Inside the function, this is the currently clicked element, and $(this) is the jQuery-selected version of that element. You can use jQuery to add and remove classes, and add and remove styles, and find elements that are the parent or children of the element you've selected.

例如,如果要查找当前选定元素的父级的"sub-menu-wrap",则可以在函数内编写var subMenu = $(this).closest('.sub-menu-wrap'),然后从那里可以检查和修改样式和课程

For example, if you want to find the 'sub-menu-wrap' that is the parent of the currently selected element, inside the function you would write var subMenu = $(this).closest('.sub-menu-wrap') and then, from there, you can check and modify styles and classes

jQuery将使您的生活更加轻松.您所遇到的每个问题也将在互联网上获得一百万个答案.

jQuery will make your life loads easier. Every question you have will have a million answers across the internet already as well.