S
S
Sweet Cookies2018-01-02 18:00:28
JavaScript
Sweet Cookies, 2018-01-02 18:00:28

A difficult ban on all links?

Friends, how to disable the transition to all links event-wise, ON pure js, WITHOUT jquery,

<a href="https://toster.ru/">toster1</a> 
<a href="https://toster.ru/">toster2</a> 
<a href="https://toster.ru/">toster3</a>

Works only with selection:
document.getElementsByTagName('a')[0].onclick = function() {
  return false;
};

or so
document.getElementByTagName('a')[0].addEventListener('click', function(e) {
   e.preventDefault();
}, false);

why doesn't work for all elements: document.getElementsByTagName('a').onclick = function()........
Tired of this simple and annoying task already! Give advice on the case, please, so as not to look for anything already !?
Here is Mlyn, everything is always simple in jquery:
$(document).on('click','a', function(){ return false; });

everything, a line of code! Native necessarily brain for ** t!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
lemme, 2018-01-02
@Sweet_Cookies

document.addEventListener('click', function(e) {
     if (e.target.tagName === 'A') {
        return e.preventDefault()
     }
})

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question