<style type="text/css">
body {     
    background:url(imgs/test.jpg) no-repeat top center  ; 
}
</style>
<script src="js/jquery-1.5.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function() {
var ImgW =   ;
alert(ImgW);
});
</script> 请问如何获取body 的背景图片的宽度和高度值?xiexie

解决方案 »

  1.   

    就如同现在的CSS还没有提供相关的属性可以让你设置背景图片的尺寸一样,你也不可能用JS通过读取某些CSS属性达到你的目地;当然,如果只是获取原始图片的尺寸,你可以new Image().src = "imgs/test.jpg"来得到...
      

  2.   

    var img = new Image();
    img.src="XXX.jpg";
    alert(img.height);
    alert(img.width);
      

  3.   

    应该就是获取body的宽高吧?
    $("body").width();  $("body").height();
    背景图片要不是这样?
    $("body").attr("background-img").width();$("body").attr("background-img").height();-------------------------上面两个都是不行的$(function(){
    var kk = $("body").attr("background");//.width();
    var img = new Image();
    img.src=kk;//------这样宽高都是0,图片没有加载完成//alert(img);
    //alert($(img).width());
    //alert(img.width);
    //alert(img.height);//------要这样img.onreadystatechange=function(){
     alert("dd");
     if (img.readyState=="complete") {
      alert(["图片大小是:",img.width,img.height]);
     }
    }});
      

  4.   

    var img = new Image();
    img.src="imgs/test.jpg.jpg"; //背景图片
    alert(img.height); //图片高度
    alert(img.width); //图片宽度
      

  5.   

    1.用jquery 取$(document.body).css("backgroundImage") 
    2.       if (window.all) {
                    imgSrc = document.body.currentStyle["backgroundImage"];
                }
                else if (window.getComputedStyle) {
                    imgSrc = window.getComputedStyle(document.body, null)['backgroundImage']
                }
      

  6.   

    1.用jquery 取$(document.body).css("backgroundImage") 
    2. if (window.document.all) {
      imgSrc = document.body.currentStyle["backgroundImage"];
      }
      else if (window.getComputedStyle) {
      imgSrc = window.getComputedStyle(document.body, null)['backgroundImage']
      }