Nous pouvons utiliserjQuerydelengthpropriété pour déterminerids'il existe :
Il peut également être écrit sous forme de plugin, comme suit, le plugin peut déterminer si un élément existe.Exemple
$.fn.exists = function(callback) {
var args = [].slice.call(arguments, 1);
if (this.length) {
callback.call(this, args);
}
return this;
};
//Utilisation
$('div.test').exists(function() {
this.append('<p>Exists!</p>');
});