Hello, who can tell me please. There is the following code.

<img src="url" class="2-5 none(это класс display:none)" /> <div id="test"> <a href="#" class="2-5">Ссылка</a> </div> 

how when clicking on a link to remove this class none? Thought to do so:

 $("#test").children("a").click(function () { $(this) return false; }); 

And then I do not understand what is contained in this this? How to find an img element with the same class? In principle, I can manipulate all the code to change classes and id, etc. Tell me how best to implement it?

  • Let's start with the fact that Названия классов могут начинаться только с буквы или подчеркивания , well, you can reach without classes: $(this).parent().prev('img') - MasterAlex

3 answers 3

For example:

 $('a.2-5').on('click', function(e){ e.preventDefault(); var img = $('img.2-5'); if(!img.hasClass('none')){ img.addClass('none'); }else { img.removeClass('none'); } }); 
 .none { display: none; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <img src="url" class="2-5 none" /> <div id="test"> <a href="#" class="2-5">Ссылка</a> </div> 

    Just add a toggle for the image, for the onclick action on the link.

    Example:

     <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script> <img src="url" class="the-same-image" /> <div id="test"> <a href="#" class="2-5" onclick="$('.the-same-image').toggle()">Ссылка</a> </div> 

      Thank you all so much for the answers!) But did it differently .. you had to do: The main image is small previews - when you click, replace the main one with a large preview. It turned out like this:

        <!DOCTYPE html> <html> <head> <title>Заголовок</title> <script src="http://code.jquery.com/jquery-latest.js"></script> </head> <body> <img src="Главная картинка первоначальная" id="img"> <a href="http://site.ru/image_big.jpg" class="url_full"><img src="http://site.ru/image_small.jpg" class="img_prew"></a> <a href="URL большого изображения" class="url_full"><img src="URL маленького изображения" class="img_prew"></a> <a href="URL большого изображения" class="url_full"><img src="URL маленького изображения" class="img_prew"></a> <a href="URL большого изображения" class="url_full"><img src="URL маленького изображения" class="img_prew"></a> <a href="URL большого изображения" class="url_full"><img src="URL маленького изображения" class="img_prew"></a> <script> $(".url_full").click(function () { var url = $(this).attr('href'); $("#img").attr('src', url); return false; }); </script> </body> </html>