If there are any tags that you know will not have a background image, you can improve the selection be excluding those with the not-selector(docs) http://api.jquery.com/not-selector/.
$('*:not(span,p)')
除此之外,您可以尝试在过滤器中使用更原生的 API 方法。
$('*').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/ http://jsfiddle.net/q63eU/
过滤器中的代码基于以下位置的 getStyle 代码:http://www.quirksmode.org/dom/getstyles.html http://www.quirksmode.org/dom/getstyles.html
发布一个for
语句版本以避免函数调用.filter()
.
var tags = document.getElementsByTagName('*'),
el;
for (var i = 0, len = tags.length; i < 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';
}
}