List of All Background Images in DOM

前端 未结 7 1122
旧巷少年郎
旧巷少年郎 2020-12-29 12:00

What\'s the best way to find all of the background images on a given page using javascript?

The ideal end result would be an array of all of the url\'s.

7条回答
  •  再見小時候
    2020-12-29 12:26

    //alert(getallBgimages())

    function getallBgimages(){
     var url, B= [], A= document.getElementsByTagName('*');
     A= B.slice.call(A, 0, A.length);
     while(A.length){
      url= document.deepCss(A.shift(),'background-image');
      if(url) url=/url\(['"]?([^")]+)/.exec(url) || [];
      url= url[1];
      if(url && B.indexOf(url)== -1) B[B.length]= url;
     }
     return B;
    }
    
    document.deepCss= function(who, css){
     if(!who || !who.style) return '';
     var sty= css.replace(/\-([a-z])/g, function(a, b){
      return b.toUpperCase();
     });
     if(who.currentStyle){
      return who.style[sty] || who.currentStyle[sty] || '';
     }
     var dv= document.defaultView || window;
     return who.style[sty] || 
     dv.getComputedStyle(who,"").getPropertyValue(css) || '';
    }
    
    Array.prototype.indexOf= Array.prototype.indexOf || 
     function(what, index){
     index= index || 0;
     var L= this.length;
     while(index< L){
      if(this[index]=== what) return index;
      ++index;
     }
     return -1;
    }
    

提交回复
热议问题