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

449
Views
Usando una declaración if para verificar si un div está vacío

Estoy tratando de eliminar un div específico si un div separado está vacío. Esto es lo que estoy usando:

 $(document).ready(function () { if ('#leftmenu:empty') { $('#menuTitleWrapper').remove(); $('#middlemenu').css({ 'right': '0', 'position': 'absolute' }); $('#PageContent').css({ 'top': '30px', 'position': 'relative' }); } });

Creo que esto está cerca, pero no puedo entender cómo escribir el código para probar que #leftmenu está vacío. ¡Cualquier ayuda es apreciada!

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Puedes usar .is() .

 if( $('#leftmenu').is(':empty') ) { // ...

O simplemente podría probar la propiedad de length para ver si se encontró una.

 if( $('#leftmenu:empty').length ) { // ...

Tenga en cuenta que vacío significa que tampoco hay espacios en blanco. Si existe la posibilidad de que haya espacios en blanco, puede usar $.trim() y verificar la longitud del contenido.

 if( !$.trim( $('#leftmenu').html() ).length ) { // ...
over 4 years ago · Santiago Trujillo Report

0

Depende de lo que entiendas por vacío.

Para verificar si no hay texto (esto permite elementos secundarios que están vacíos):

 if ($('#leftmenu').text() == '')

Para verificar si no hay elementos secundarios o texto:

 if ($('#leftmenu').contents().length == 0)

O,

 if ($('#leftmenu').html() == '')
over 4 years ago · Santiago Trujillo Report

0

Si desea una demostración rápida de cómo verificar divs vacíos, le sugiero que pruebe este enlace:

http://html-tuts.com/check-if-html-element-is-empty-or-has-child-tags/


A continuación tienes algunos ejemplos breves:

Usando CSS

Si su div está vacío sin nada, incluso sin espacios en blanco, puede usar CSS:

 .someDiv:empty { display: none; }

Desafortunadamente, no hay un selector de CSS que seleccione el elemento hermano anterior. Solo hay para el siguiente elemento hermano: x ~ y

 .someDiv:empty ~ .anotherDiv { display: none; }

Usando jQuery

Comprobación de la longitud del texto del elemento con la función text ()

 if ( $('#leftmenu').text().length == 0 ) { // length of text is 0 }

Compruebe si el elemento tiene etiquetas de niños dentro

 if ( $('#leftmenu').children().length == 0 ) { // div has no other tags inside it }

Compruebe si hay elementos vacíos si tienen espacios en blanco

 if ( $.trim( $('.someDiv').text() ).length == 0 ) { // white-space trimmed, div is empty }
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!