[英]Change Menu Styling on fly or pass a param inside jQuery .each iterator
I have a bunch of menu links and want to change their style on click - say you click "about" and it becomes bold and red. 我有一堆菜单链接,并且想在单击时更改其样式-说您单击“关于”,它会变成粗体和红色。 I select the items and bind click event to them:
我选择项目并将click事件绑定到它们:
$("#nav_menu > *").bind("click",function(){doTrigger(this.id);});
this way I pass the ID of the clicked item to doTrigger
. 这样,我将单击项的ID传递给
doTrigger
。
Ok. 好。 Now in
doTrigger
I am trying to iterate through the items and change their styles: all to style1 and clicked to style2 for example. 现在,在
doTrigger
我尝试遍历所有项目并更改其样式:例如全部更改为style1并单击为style2 。 The problem is that: 问题是:
$("#nav_menu > *").each(function(){;});
will not let me pass the id of the clicked item. 不会让我传递点击项的ID。
I think there should be a less complicated way of getting what I need. 我认为应该有一种不太复杂的方式来获取我所需要的东西。 Besides, I think I am lost, too.
此外,我想我也迷路了。
the function passed to $.each()
in doTrigger()
runs in the context of and thus has access to variables in doTrigger()
- you don't need to pass anything 传递给函数
$.each()
在doTrigger()
中的上下文中运行,因此访问变量doTrigger()
-你不需要通过任何
function doTrigger(id) {
...
$("#nav_menu > *").each(function(){ /* you can access var id in here */ ;});
Try something like this: 尝试这样的事情:
$("#nav_menu > *").bind("click",function(){ $("#nav_menu > *").attr('class', 'class1'); $(this).attr('class', 'class2'); });
This resets all children to class1
when an item is clicked, and then only that item is class2
. 单击某个项目时,这会将所有子级重置为
class1
,然后只有该项目为class2
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.