How can I determine the background image URL of a div via JavaScript?

后端 未结 5 908
陌清茗
陌清茗 2020-12-17 22:58

I\'ve found plenty of information on how to change the background image of a div using JavaScript, but I am trying to use JavaScript to determine which background image is b

5条回答
  •  感情败类
    2020-12-17 23:20

    this should do it

    alert( document.getElementById("widgetField").style['background-image'] );
    

    You can do the following to get all the style properties after you have made changes ... so you see where the new things went ...

      var style = document.getElementById("widgetField").style;
      var allprops = '';
      for  ( i in style )
        allprops += style[i] + ':' + i + '\n' ;
      alert( allprops );
    

    [EDIT] as i go along i will add here ..
    Google Chrome: style['background-image']
    Firefox 3.5.7: style.background
    IE 6: style.background

    style.backgroundImage work on all browsers ( IE6, IE7, FF 3.5.7, Google Chrome, Opera 10.10, Safari 4.0.4) for Windows ..

提交回复
热议问题