javascript - 将子图像的宽度和高度应用于容器

标签 javascript jquery html

我有这个标记:

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

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

图像位于绝对位置

最佳答案

未经测试,但我建议:

$('.img_container img').each(function(){
    var w = this.naturalWidth,
        h = this.naturalHeight;
    $(this).parent().css({'height': h, 'width' : w});
});

关于javascript - 将子图像的宽度和高度应用于容器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15345926/

相关文章:

jquery - 在汉堡菜单中隐藏列表项

javascript - HTML - 将行放入嵌套表中

javascript - 在 rails 上使用react :Iterating through an array of activerecord

javascript - Ajax 帖子不工作 codeigniter

javascript - Jquery循环遍历表中的所有行,没有第一行

html - 两个具有自动宽度的 DIV

html - CSS a 标签没有排列

javascript - 函数中的 JQuery 复选框双重操作

javascript - Junit GWT 错误 - 在对象 [对象窗口] 中找不到函数 attachEvent

jquery - 如何使用 jQuery 访问 "style"属性中的属性?