Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

315
Visualizações
difference between preventDefault() and off('click')

I use on() JQuery function and preventDefault() for prevent anchor default action. I can not understand off() JQuery function. can I use off('click') instead of:

$('.anchor').on('click', function(e) {
  e.preventDefault();
});

I mean, can I use this for prevent default action of anchor?

$('.anchor').off('click');
over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

No, as per the documentation of .off():

The .off() method removes event handlers that were attached with .on().

So off() only removes handlers that were added by jQuery. But default handlers, e.g. on a link or on a submit button, must be prevented by preventDefault().

over 4 years ago · Santiago Trujillo Relatório

0

off : disable the eventHandler so no function can be execute on click preventdefault : disable the default eventHandler but you can use your own function.

over 4 years ago · Santiago Trujillo Relatório

0

They are logically very different.

preventDefault() stops the browser from acting on the event behaviour but still executes the JS code. For example, in the below, the URL the .anchor element points to will not be transferred, however the JS code will still run:

$('.anchor').on('click',function(e){
    e.preventDefault();
    console.log('the browser wont redirect, but you will see this line in the console')
});

On the other hand, off() completely removes any event handler of the given type from the element. In this case, the browser will be transferred to the given URL on click and no JS code will be run.

$('.anchor').off('click');
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda