Hello.

I have this problem. There is a menu:

<ul id="menu"> <li id="1">Ссылка1<span class="list1">[<span id="list1">0</span>]</span></li> <li id="2">Cсылка2<span class="list1">[<span id="list2">0</span>]</span></li> <li id="3">Ссылка3<span class="list1">[<span id="list3">0</span>]</span></li> <li id="4">Ссылка4<span class="list1">[<span id="list4">0</span>]</span></li> <li id="5">Ссылка5<span class="list1">[<span id="list5">0</span>]</span></li> </ul> 

Do not tell me how to verify the menu item with all the clicked? If it is, then assign it a certain value.

  $('li').click(function() { var current = $(this); // текущая позиция var id = current.attr('id'); // текущий id var str = "#list"+ id; var number = parseInt($(str).text()); // изменение индекса 

Here's how to search the entire list? If the current (pressed) id coincides with any of the menus, then perform certain actions.

  • I think it is necessary to create an array of id and compare the pressed item with the elements of the array; if there is a match, we perform actions. - LeD4eG
  • one
    If it is not difficult - explain what should happen to HTML after clicking, but it is not entirely clear. - chernomyrdin 1:59 pm
  • This is what the menu should do. <br> 1. When you click on a link, all others are shifted to the side. <br> 2. When you click on another link, first everyone returns to the original position, and then they shift relative to the current one, as in paragraph 1. <br> 3. After clicking on a link, a click counter is added to its text (for example, “Link [2]”), and the value of the counter increases with each click. Before the first click, the link of the counter does not have (“Link”). <br> 4. After reaching the threshold value of the click counter (5), the link disappears from the list. - murad30

1 answer 1

Filter your set of DOM elements, if the length is greater than one, then there is a repetition:

 ... var id = current.attr('id');//ваш индекс if($('li').filter(function(index){ return $(this).attr('id')==id;//можно проще }).length > 1) //проверяем сколько li-шек с "вашим индексом" в списке { //выполнить определенные действия } ... 
  • var id = current.attr ('id'); // index <br> $ (this) .css ('margin-left', '0'); // for the current value <br> if (id == 1) // when the 1st link is selected <br> {while (i <5) {// pass to the end of the list <br> current.next (). animate ({ marginLeft: '20px'}, {duration: 200}); <br> current = current.next () <br> if (i == 5); <br> i ++; <br>}} <br> Something happens for each case. Please tell me how to do it: <br> When you click on another link, everyone returns to the starting position and then shifts relative to the current one. , as in paragraph 1 Thanks in advance! - murad30
  • I'm trying to do the following <br> 1) Get the current clicked on <br> 2) using .next () to find its neighbor which is below, shift it <br> 3) do it in the while loop by shifting all its neighbors from below while they does not end there <br> 4) is similar with the neighbors above using .prev () <br> How to implement it. - murad30
  • if (number <5) {<br> $ (str) .html (number); $ (this) .find ('span.list1'). show () <br> $ ('li'). each (function () {$ (this) .css ('margin-left', '0px') ;}); <br> while (current.next (). length! = 0) {<br> current.next (). animate ({marginLeft: '20px'}, {duration: 200}); <br> current = current.next ();} <br> while (current1.prev (). length! = 0) {<br> current1.prev (). animate ({marginLeft: '20px'}, {duration: 200} ); <br> current1 = current1.prev ();}} <br> else {$ (this) .remove (); }}); <br> - murad30