console.log(document.getElementById("mainnav").style)document.getElementById("id").style.backgroundImagebackground获取

解决方案 »

  1.   

    写在样式表里的样式是没办法直接用style来获取的,,具体的可以自己百度一下currentStyle跟getComputedStyle。
      

  2.   

    通过dom的stlye属性或者jquery的$().css(background)
      

  3.   

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>无标题文档</title>
    <style type="text/css">
    div{
    width:400px;
    height:400px;
    background-image:url(1.jpg);
    background-repeat:no-repeat;
    background-color:#933;
    background-position:center;
    }
    </style>
    <script type="text/javascript">
    function init(){
    var a=document.getElementById("test");
    var style="";
    if(document.defaultView&&document.defaultView.getComputedStyle){
    style=document.defaultView.getComputedStyle(a);
    }else{
    style=a.currentStyle;
    }
    alert(style.backgroundImage);
    }
    window.onload=init;
    </script>
    </head><body>
    <div id="test"></div>
    </body>
    </html>
    类似这样试试
      

  4.   

    我想实现的是利用img图片加载来显示网页的加载状态百分比貌似有点问题,背景的没有用上,
    这是代码,以后再完善下
    (function( window, undefined ) {});
    var class2type = {},tmp=[],
    core_toString = class2type.toString,
    core_push   = class2type.push;
    Array.prototype.del = function() {   
    var a = {}, c = [], l = this.length; 
    for (var i = 0; i < l; i++) {
      var b = this[i].src||this[i];
      var d = (typeof b) + b;
      if (a[d] === undefined) {
    c.push(this[i]);
    a[d] = 1;
      }
    }
    return c;   
    }  var pageload={set:{class:"*",color:"red"},
    type: function( obj ) {
    if ( obj == null ) {return String( obj );}
    return typeof obj === "object" || typeof obj === "function" ? class2type[ core_toString.call(obj) ] || "object" : typeof obj;
    },
    isFunction: function( obj ) {return this.type(obj) === "function";},isArray: Array.isArray || function( obj ) {return this.type(obj) === "array";},isWindow: function( obj ) {return obj != null && obj == obj.window;},isNumeric: function( obj ) {return !isNaN( parseFloat(obj) ) && isFinite( obj );},isArraylike:function ( obj ) { var length = obj.length,type = this.type( obj ); if ( this.isWindow( obj ) ) {return false;} if ( obj.nodeType === 1 && length ) { return true;} return type === "array" || type !== "function" && ( length === 0 || typeof length === "number" && length > 0 && ( length - 1 ) in obj );
    },
    makeArray:function(a,b){
    var list=b||[];
    if ( a != null ) {if ( this.isArraylike(Object(a)) ) { this.merge(list, typeof a === "string" ? [ a ] : a );} else { core_push.call( list, a ); }}
    return list;
    },
    merge: function( first, second ) {
    var l = second.length,
    i = first.length,
    j = 0; if ( typeof l === "number" ) {
    for ( ; j < l; j++ ) {
    first[ i++ ] = second[ j ];
    }
    } else {
    while ( second[j] !== undefined ) {
    first[ i++ ] = second[ j++ ];
    }
    } first.length = i; return first;
    },
    getStyles : function( elem ) { try{
    if(window.getComputedStyle)
    return window.getComputedStyle( elem, null );
    else alert("err")
    }catch(e){alert("aa")}
    },
    byClass: function(sClass, oParent) {
    var aClass = [],tm,imgobj;
    var reClass = new RegExp("(^| )" + sClass + "( |$)");
    var aElem = this.byTagName("*", oParent);
    for (var i = 0; i < aElem.length; i++) {
    if(reClass.test(aElem[i].className) && this.getStyles(aElem[i]).backgroundImage!=="none" ){
    tm=this.getStyles(aElem[i]).backgroundImage.replace(/url\([\"\']?/g,"").replace(/[\"\']?\)/g,"");
    imgobj=new Image();
    imgobj.src=tm;aClass.push(imgobj);
    }
    //reClass.test(aElem[i].className) && aClass.push(aElem[i]);
    }
    return aClass
    },
    byTagName: function(elem, obj) {
    var list=[];
    return this.makeArray((obj || document).getElementsByTagName(elem),list)
    },
    creatPageLoad:function(){
    var div=document.createElement("div");
    div.id="pageLoad";
    div.style.height=3;
    div.style.position="absolute";
    div.style.backgroundColor=this.set.color;
    div.style.top=0;div.style.zIndex=100;
    document.body.appendChild(div);
    return div;
    },show:function(num,obj){
    var tmp=obj.style.width;
    var sss2=document.getElementById("sss2")
    sss2.innerHTML=obj.style.width;
    obj.style.width=((num*100)>=1?100:(num*100).toFixed(2))+"%";
    //console.log('3.'+obj.style.width)
    //if(num>=1)this.hide(obj)
    },
    hide:function(obj){ obj.style.display="none";},
    init:function(){
    var imglist,sums=0,wh=0,_this=this;imglist=_this.byTagName("img"),j=0;
    var sss=document.getElementById("sss");
    var sss2=document.getElementById("sss2");
    imglist=_this.makeArray(_this.byClass(_this.set.class),imglist); imglist=imglist.del();
    var pageLoad=pageLoad||_this.creatPageLoad();
    for(var i=0;i<imglist.length;i++){
    // if(imglist[i].width||imglist[i].height){
    try{
    if(imglist[i].complete) {
    console.log('1.'+(imglist[i].width||imglist[i].style.width||1)+','+imglist[i].height+','+sums+','+imglist[i].src)
    }else{
    console.log('2.'+(imglist[i].width||imglist[i].style.width||1)+','+imglist[i].height+','+sums+','+imglist[i].src) }}catch(e){alert("console.log error")}
    sums+=(imglist[i].width*imglist[i].height);

    }
    //console.log('s'+wh+','+sums);
    for(var i=0;i<imglist.length;i++){
    //document.write(imglist[i].src+','+ imglist[i].width*imglist[i].height+'<br>');

    if(imglist[i].complete) {
    wh+=(imglist[i].width*imglist[i].height);
    //console.log('1.'+wh/sums)
    _this.show(wh/sums,pageLoad);
    }else{
    imglist[i].onload=function(){
    wh+=(this.width*this.height);
    //console.log(((wh/sums*100).toFixed(2))+','+''+''+this.width+','+this.height)
    _this.show(wh/sums,pageLoad);
    //if(wh/sums>=1)console.log(sums);
    this.onload=null;
    }
    }
    }
    //console.log(wh+','+sums) tmp=imglist;
    //document.write(imglist.length);
    }
    }
    window.pl=pageload;
    pl.fn=pl.prototype;pl.init();
    //document.write(pl.byTagName("img").length)