Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

107
Views
Reemplace los controladores de múltiples clics con un controlador de un solo clic

Me pregunto si es posible limpiar un poco estos eventos de clic:

 $(document) .on('click', '.newGameItem', getNewGameItem) .on('click', '.startGame', startThisGame) .on('click', '.showThisGameInfo', showThisGameInfo) .on('click', '.closeThisGameInfo', closeThisGameInfo) .on('click', '#api_game_test', testApiGame) .on('click', '.nextGame', shiftGames) .on('click', '.confirm_cancel', cancelLexTimer) .on('click', '.completed_submission', reload) .on('turbolinks:load', function() { ... });

Los primeros 8 métodos en la cadena son eventos de clic: ¿hay alguna forma en javascript de combinarlos bajo un "paraguas" de un clic, aunque los eventos de clic se deleguen en diferentes elementos?

about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Puede usar un solo .click y verificar el objetivo del clic con el argumento del event :

 $(document).on('click', function (event) { var target = event.target; if ($(target).hasClass('newGameItem')) getNewGameItem(); if ($(target).hasClass('startGame')) startThisGame(); if (target.id == 'api_game_test') testApiGame(); // etc })

Aquí está el documento (bastante pobre) sobre event.target de jQuery: api.jquery.com/event.target

about 4 years ago · Santiago Trujillo Report

0

No vas a escaparte de un gran bloque de algo , ya sea .on o la respuesta de GG . Siempre puede colocarlo en un archivo .js separado para separarlo y que no abarrote su código.

 // in addHandlers.js function addHandlers(document) { document.on('click',... }

Esto limpiará su código mientras elimina todo el desorden no esencial mientras hace las cosas divertidas.

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!