jquery check if selected element exists

Sometimes we need to judge whether the element selected by the jquery selector exists. If it exists, some operations will be performed. If it does not exist, it will not be performed. So how to judge whether the element exists, the code is as follows:

// Determine whether there is an element with a specific ID value 
if ($( " #id " ).length > 0 ){
    alert( " exists " );
}else{
    alert( " doesn't exist " );
}
// Determine whether there is a specific label element 
if ($( " label name (such as label) " ).length > 0 ){
    alert( " exists " );
}else{
    alert( " doesn't exist " );
}
// Determine whether there is an element under an element 
if ($( " #element ID " ).find( " ID to be determined " ).length > 0 ){
    alert( " exists " );
}else{
    alert( " doesn't exist " );
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325609883&siteId=291194637