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

474
Views
jQuery: obtener el nombre de la etiqueta del elemento seleccionado

¿Hay una manera fácil de obtener un nombre de etiqueta?

Por ejemplo, si me dan $('a') en una función, quiero obtener 'a' .

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Puede llamar a .prop("tagName") . Ejemplos:

 jQuery("<a>").prop("tagName"); //==> "A" jQuery("<h1>").prop("tagName"); //==> "H1" jQuery("<coolTagName999>").prop("tagName"); //==> "COOLTAGNAME999"


Si escribir .prop("tagName") es tedioso, puede crear una función personalizada como esta:

 jQuery.fn.tagName = function() { return this.prop("tagName"); };

Ejemplos:

 jQuery("<a>").tagName(); //==> "A" jQuery("<h1>").tagName(); //==> "H1" jQuery("<coolTagName999>").tagName(); //==> "COOLTAGNAME999"


Tenga en cuenta que, por convención, los nombres de las etiquetas se devuelven en MAYÚSCULAS . Si desea que el nombre de la etiqueta devuelta esté en minúsculas, puede editar la función personalizada de la siguiente manera:

 jQuery.fn.tagNameLowerCase = function() { return this.prop("tagName").toLowerCase(); };

Ejemplos:

 jQuery("<a>").tagNameLowerCase(); //==> "a" jQuery("<h1>").tagNameLowerCase(); //==> "h1" jQuery("<coolTagName999>").tagNameLowerCase(); //==> "cooltagname999"
over 4 years ago · Santiago Trujillo Report

0

Puede usar la propiedad nodeName del DOM:

 $(...)[0].nodeName
over 4 years ago · Santiago Trujillo Report

0

A partir de jQuery 1.6, ahora debe llamar a prop:

 $target.prop("tagName")

Ver http://api.jquery.com/prop/

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!