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

191
Views
JavaScript: función onclick para cambiar otros elementos onclick

Funcionalidad prevista

Estoy buscando algo que funcione así:

  1. El usuario hace clic en el img
  2. Se llama a la función de clic manual y 'habilita' el ancla principal del img
  3. Si se vuelve a hacer clic, el ancla redirigirá al usuario a una nueva página

Mi problema:

Lo siguiente 'habilita' el enlace, pero también actúa como si se hiciera clic en el enlace al mismo tiempo.

¿Hay alguna forma de arreglar esta funcionalidad?

Código HTML:

 <a href="@Url.Action("Index/" + @card.ID)" onclick="return false;" class="link"> <img src="~/Resources/@card.Image" id="@card.ID" onclick="handClick(@((int)card.Value), @card.ID)" class="card" /> </a>

Código JavaScript

 function handClick(cardValue, cardID) { *... irrelevant code ...* var playedCard = document.getElementById(cardID); *... irrelevant code ...* playedCard.parentElement.onclick = function () { return true }; }
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Como no sé cuál es el código irrelevante, me quedaré solo en el problema de habilitar el primer clic.

Mi enfoque incluye el atributo href . sin ella el enlace no funciona. Entonces, lo que hice fue cambiar el nombre del atributo a otra cosa y cuando se hizo clic, lo reemplazó con el atributo real, "activando" así el enlace.

Observe que el cursor cambia a un puntero después del primer clic.

 document.querySelectorAll('a[hrefx]').forEach(function(el) { el.addEventListener('click', function() { // This condition enable re-using the listener for other purposes. if (this.getAttribute('hrefx')) { this.setAttribute('href', this.getAttribute('hrefx')); this.removeAttribute('hrefx'); } }); });
 <a hrefx="#"> <img src="https://picsum.photos/200"> </a> <a hrefx="#"> <img src="https://picsum.photos/200"> </a>

about 4 years ago · Juan Pablo Isaza 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!