1

我有这个标记:

    <div class="img_container">
        <img src="file://localhost/img/a_bg.jpeg" alt="a_bg" />
    </div>

如何将图像的宽度和高度应用到 img_container?

图像在绝对位置

4

3 回答 3

1

未经测试,但我建议:

$('.img_container img').each(function(){
    var w = this.naturalWidth,
        h = this.naturalHeight;
    $(this).parent().css({'height': h, 'width' : w});
});
于 2013-03-11T18:40:45.053 回答
0

标记:

您需要id在图像中添加一个,如下所示:

<div class="img_container">
        <img id="bgimg" src="file://localhost/img/a_bg.jpeg" alt="a_bg" />
    </div>

查询:

var img = $('#bgimg'); 

img.load(function(){
     //Get width of image
     var width = img.width();
     //Get height of image
     var height = img.height();
     $('.img_container').css("width", width);
     $('.img_container').css("height", height);
});
于 2013-03-11T18:36:09.090 回答
0

尝试这个

<img src="file://localhost/img/a_bg.jpeg" alt="a_bg width="X" height="Y" />

并将其添加到您的 CSS

background-image: url(file://localhost/img/a_bg.jpeg);
background-size: Xpx Ypx;
background-repeat:no-repeat;
于 2013-03-11T18:38:04.887 回答