[英]How can i make my jQuery code work on all elements
I have this accordion menu but it only works on the first ul
.我有这个手风琴菜单,但它只适用于第一个
ul
。 How do I get it to work on all ul
like this?我如何让它像这样在所有
ul
上工作? If you could explain what I'm doing wrong so I know in the future that would be great.如果你能解释我做错了什么,那么我知道将来会很棒。
Also, how do I get it so that the link toggles between two classes, right
and down
each time it is clicked?另外,我如何获得它以便每次单击时链接在两个类之间切换,
right
和down
? It also has the class turq-font
on it.它还具有
turq-font
类。 I want the right
to be removed and replaced with down
when the link is clicked and the menu is showing.当单击链接并显示菜单时,我希望删除该
right
并用down
替换。 Heres my code:这是我的代码:
$(function() { $("#show-menu").click(function() { $("#sub-menu-acc").toggleClass("active-menu", 1000); }); });
.active-menu { display: block !important; } .admin-area ul li { margin: 6px 0px; } .admin-right-menu ul li a { color: black; text-decoration: none; } .admin-area ul { list-style: none; margin-bottom: 20px; } .admin-area ul li ul { display: none; padding-left: 20px; margin-bottom: 0px; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div class="admin-area"> <div class="admin-right-menu"> <ul> <li><a href="#" id="show-menu" class="turq-font right">Stats ></a> <ul id="sub-menu-acc"> <li><a href="#">Business Stats</a> </li> <li><a href="#">Affiliate stats</a> </li> </ul> </li> <li><a href="#" id="show-menu" class="turq-font right">Reports ></a> <ul id="sub-menu-acc"> <li><a href="#">Global</a> </li> <li><a href="#">Sales</a> </li> <li><a href="#">Sales trends</a> </li> </ul> </li> </ul> </div></div>
The issue is due to your use of repeated id
attributes;问题是由于您使用了重复的
id
属性; they should be unique within a document
.它们在
document
应该是唯一的。 Convert your code to use classes instead.将您的代码转换为使用类。
Also note that you should use the this
keyword to traverse the DOM to find the ul
related to the clicked a
element, and the addition of right down
in a toggleClass()
call.另请注意,您应该使用
this
关键字遍历 DOM 以查找与单击a
元素相关的ul
,并在toggleClass()
调用中添加right down
。 Try this:尝试这个:
$(function() { $(".show-menu").click(function(e) { e.preventDefault(); // stop the '#' of the clicked a appearing in the URL $(this).toggleClass('right down').next(".sub-menu-acc").toggleClass("active-menu"); }); });
.active-menu { display: block !important; } .admin-area ul li { margin: 6px 0px; } .admin-right-menu ul li a { color: black; text-decoration: none; } .admin-area ul { list-style: none; margin-bottom: 20px; } .admin-area ul li ul { display: none; padding-left: 20px; margin-bottom: 0px; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div class="admin-area"> <ul> <li> <a href="#" class="show-menu turq-font right">Stats ></a> <ul class="sub-menu-acc"> <li> <a href="#">Business Stats</a> </li> <li> <a href="#">Affiliate stats</a> </li> </ul> </li> <li> <a href="#" class="show-menu turq-font right">Reports ></a> <ul class="sub-menu-acc"> <li> <a href="#">Global</a> </li> <li> <a href="#">Sales</a> </li> <li> <a href="#">Sales trends</a> </li> </ul> </li> </ul> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.