Tarkista, onko elementti olemassa - CSS-temppuja

Anonim
if ($('#myElement').length > 0) ( // it exists )

Tai tehdä siitä hieno toiminto, jossa on takaisinsoitto:

// Tiny jQuery Plugin // by Chris Goodchild $.fn.exists = function(callback) ( var args = ().slice.call(arguments, 1); if (this.length) ( callback.call(this, args); ) return this; ); // Usage $('div.test').exists(function() ( this.append('

I exist!

'); ));