Movement of HTML page

Asked

Viewed 47 times

0

When I click on an anchor like <a href="#um_id">clique aqui</a>, that has a #ID on href, it defaults to move up to the part that has this id as a bookmark.

How I would discount this behavior?

  • And what do you intend to do at the anchor click? Anchors were made for navigation, if the idea is to perform some function javascript in the click, use a button and, by default, will not have the problem cited.

  • Do you want to break the anchor? Just put an id that doesn’t exist on the link href, like #! For example, or remove the id of the html tag from the element. your question was not very clear.

1 answer

0

You can disable the standard anchor action (either for the document itself or a normal link) in two ways. My examples will affect all the page anchors, it’s up to you to adapt the selectors to your need.

With CSS

a { pointer-events: none; }

https://jsfiddle.net/bfavaretto/yu7rL2n4/

With JS

const stopClick = (e) => e.preventDefault()
document.querySelectorAll('a').forEach( a => a.addEventListener('click', stopClick, false));

https://jsfiddle.net/bfavaretto/y2hqpbtf/

Browser other questions tagged

You are not signed in. Login or sign up in order to post.