Sprawdź czy element istnieje w jQuery [duplikat]

To pytanie ma już odpowiedź tutaj:

Jak sprawdzić, czy element istnieje, jeśli element jest tworzony metodą .append()? Nie działa na mnie.

Author: Matt, 2011-01-04

8 answers

$('elemId').length nie działa na ja.

Musisz umieścić # przed ID elementu:

$('#elemId').length
---^

Z vanilla JavaScript, nie potrzebujesz hash (#) np. document.getElementById('id_here'), jednak podczas korzystania z jQuery, musisz umieścić hash do elementów docelowych opartych na id, tak jak CSS.

 1478
Author: Sarfraz,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-10-29 11:48:19

Spróbuj sprawdzić długość selektora, jeśli zwróci ci coś, to element musi istnieć inaczej.

 if( $('#selector').length )         // use this if you are using id to check
{
     // it exists
}


 if( $('.selector').length )         // use this if you are using class to check
{
     // it exists
}
 301
Author: Tapan kumar,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-10-02 19:22:08

Spróbuj tego:

if ($("#mydiv").length > 0){
  // do something here
}

Właściwość length zwróci zero, jeśli element nie istnieje.

 97
Author: Gaurav123,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-08-12 09:34:27

Jak sprawdzić czy element istnieje

if ($("#mydiv").length){  }

Jeśli jest 0, to oceni do false, cokolwiek więcej niż true.

Nie ma potrzeby większego niż, mniej niż porównania.

 53
Author: lfender6445,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-07-08 05:49:55

Twój {[2] } Jak sama nazwa wskazuje, jest atrybutem Id, to wszystko, co możesz zrobić, aby sprawdzić, czy istnieje:

Vanilla JavaScript: W przypadku, gdy masz bardziej zaawansowane selektory:

//you can use it for more advanced selectors
if(document.querySelectorAll("#elemId").length){}

if(document.querySelector("#elemId")){}

//you can use it if your selector has only an Id attribute
if(document.getElementById("elemId")){}

JQuery:

if(jQuery("#elemId").length){}
 24
Author: Mehran Hatami,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-06-22 06:29:35

Możesz również użyć notacji przypominającej tablicę i sprawdzić, czy pierwszy element jest ustawiony. Pierwszy element pustej tablicy lub kolekcji jest po prostu undefined, więc otrzymujesz" normalne " zachowanie JavaScript prawdziwe/fałszywe:

var el = $('body')[0];
if (el) {
    console.log('element found', el);
}
if (!el) {
    console.log('no element found');
}
 11
Author: loopmode,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-09-17 21:24:07

Możesz użyć natywnego JS do sprawdzenia istnienia obiektu:

if (document.getElementById('elemId') instanceof Object){
    // do something here
}

Nie zapominaj, że jQuery jest niczym więcej niż wyrafinowanym (i bardzo użytecznym) opakowaniem wokół natywnych poleceń i właściwości Javascript

 7
Author: Webomatik,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-05-12 13:37:10

Jeśli masz klasę na swoim elemencie, możesz spróbować:

if( $('.exists_content').hasClass('exists_content') ){
 //element available
}
 -13
Author: DesignForLife,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-01-29 10:25:43