[英]prototype js library beginner
I have a list in which I am dynamically adding the classname, now when I click on that item again the class stays there. 我有一个列表,我在其中动态添加类名,现在当我再次点击该项时,该类保持在那里。 I dont want that I want that classname to be assigned to the currently clicked list item.
我不希望我希望将该类名分配给当前单击的列表项。
<ul id="menubar">
<li>one</li>
<li>two</li>
</ul>
Now when I click on one I dynamically add classname, and when I click on two the classname stays the same on one and it gets affected on two as well. 现在,当我点击一个动态添加类名时,当我点击两个时,类名在一个上保持不变,它也会受到影响。 but I want it to be removed from one and to be applied to currently clicked item.
但我希望将其从一个中移除并应用于当前单击的项目。
How can I achieve it? 我怎样才能实现它?
$$('#menubar li').invoke('observe', 'click', (function(e) {
//removes classname from all the li elements
$$('#menubar li').invoke('removeClassName', 'myClass');
//Gets the li you clicked on
var list_item = Event.element(e);
//adds the classname
list_item.addClassName('myClass');
}).bindAsEventListener(this));
You need to be including .removeClass() in the same event as when you addClass() to the other one. 您需要在与addClass()到另一个事件时相同的事件中包含.removeClass()。
$('.one').click(function(){
$(this).addClass('myClass');
$('.two').removeClass('myClass');
});
This could be written more efficiently, but im writing this in between meetings. 这可以更有效地编写,但我在会议之间写这个。
Hope it's a start! 希望这是一个开始!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.