jquery - Quickly select all elements with css background image -
i want grab elements on page have css background-image. can via filter function, it's slow on page many elements:
$('*').filter(function() { return ( $(this).css('background-image') !== '' ); }).addclass('bg_found');
is there faster way select elements background images?
if there tags know not have background image, can improve selection excluding not-selector
(docs).
$('*:not(span,p)')
aside that, try using more native api approach in filter.
$('*').filter(function() { if (this.currentstyle) return this.currentstyle['backgroundimage'] !== 'none'; else if (window.getcomputedstyle) return document.defaultview.getcomputedstyle(this,null) .getpropertyvalue('background-image') !== 'none'; }).addclass('bg_found');
example: http://jsfiddle.net/q63eu/
the code in filter based on getstyle code from: http://www.quirksmode.org/dom/getstyles.html
posting for
statement version avoid function calls in .filter()
.
var tags = document.getelementsbytagname('*'), el; (var = 0, len = tags.length; < len; i++) { el = tags[i]; if (el.currentstyle) { if( el.currentstyle['backgroundimage'] !== 'none' ) el.classname += ' bg_found'; } else if (window.getcomputedstyle) { if( document.defaultview.getcomputedstyle(el, null).getpropertyvalue('background-image') !== 'none' ) el.classname += ' bg_found'; } }
Comments
Post a Comment