PodemosjQuerydelengthcomprobar el atributoidsi existe:
También se puede escribir como un plugin, como se muestra a continuación. El plugin puede comprobar si el elemento existe.Ejemplo
$.fn.exists = function(callback) {
var args = [].slice.call(arguments, 1);
if (this.length) {
callback.call(this, args);
}
return this;
};
//Método de uso
$('div.test').exists(function() {
this.append('<p>¡Existe!</p>');
});