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

230
Views
¿Cómo selecciono todas las anclas con un href específico?

Tengo el siguiente código para rastrear páginas vistas para enlaces externos que coincidan con una URL en particular.

 $("a").each(function(i){ if ( $(this).attr('href') == "http://example.com/external/link/" || $(this).attr('href') == "http://example.com/external/link" ) { $(this).click(function(){ _gaq.push(['_trackPageview', '/external/pagename']); }); } });

Este código funciona, pero es extremadamente ineficiente para páginas con muchos enlaces. ¿Hay alguna manera de usar un selector para seleccionar todos los anclajes con hrefs coincidentes en lugar de escanear todos los enlaces en la página?

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Puede usar el selector de atributo comienza con

 $('a[href^="http://example.com/external/link"]').click(function() { _gaq.push(['_trackPageview', '/external/pagename']); });
over 4 years ago · Santiago Trujillo Report

0

Sí

 $('a[href^="http://example.com/external/link"]').click(function() {});

Usando el selector de atributos, puede buscar un href en particular. En lugar del href= normal que podría esperar, he usado href^= que coincide con cualquier elemento que comience con la cadena especificada.

Además, no necesita usar each para enlazar con el evento de clic de todas las etiquetas de anclaje que seleccionará. click() lo hará automáticamente por usted.

 $("a[href^="http://example.com/external/link"]").click(function(){ _gaq.push(['_trackPageview', '/external/pagename']); });
over 4 years ago · Santiago Trujillo Report

0

En jQuery, obtener todos los hrefs sería como:

 var href = 'http://www.google.com'; var elements = $('a[href=' + href + ']'); alert("Found: " + elements.length);
over 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!