In the menu, category items have a # # lattice and, because of this, when you click a person, flips up the pages. Found this to be removed by adding onclick="return false" :

 < a href="#" onclick="return false" > link < /a> 

Tell me how to fix it without adding an onclick link to each link, but setting a rule once? For example, in js for links inside a class such and such. Or somehow in a different way.

  • document.querySelectorAll("a[href='#']").forEach(el => { el.addEventListener("click", () => 0); }) - Artem Gorlachev

2 answers 2

I would suggest the option of delegating a click on the document and there checking

 $(function () { $(document).click((e) => { const {target} = e; if(target.nodeName === 'A' && target.getAttribute('href') === '#') { e.preventDefault(); } }); }); 
 a { display: block; width: 100%; height: 250px; font-size: 20px; line-height: 20px; text-align: center; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <a href='#'>Ссылка 1</a> <a href='#'>Ссылка 2</a> <a href='#'>Ссылка 3</a> <a href='#'>Ссылка 4</a> <a href='#'>Ссылка 5</a> <a href='#'>Ссылка 6</a> <a href='#'>Ссылка 7</a> <a href='#'>Ссылка 8</a> <a href='#'>Ссылка 9</a> <a href='#'>Ссылка 10</a> 

Will work for all links with href=# , even if the links are added after the document is loaded.

  • Something did not react to your code. I insert it into the js file inside $ (function () {......}); - Vadim Shorohov
  • @ Vadim Shorokhov corrected the answer - ThisMan
  • It works, thanks! - Vadim Shorohov
 link = document.querySelector("a[href=#]"); function stopAnchor(e) { e.preventDefault(); } link.onclick = stopAnchor;