jQuery获取图像尺寸,应用于Div [英] jQuery Get Image Dimensions, Apply To Div

查看:69
本文介绍了jQuery获取图像尺寸,应用于Div的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望div能够获得环绕的图像的内联样式.

笔在这里: http://codepen.io/stevenmorgan94/pen/xuEwm

HTML

<div class="box">
  <img src="http://placehold.it/250x150" />
</div>

JS

var img = $(".box > img");
$(".box").css({width:img.width(), height:img.height()});

因此jquery可以获取图片尺寸,并在.box中添加宽度和高度的内联样式

解决方案

jonathan的理解不正确.他的解决方案与询问的内容无关.正确的是

var img = $(".box > img");
$(".box").css({width:img.width(), height:img.height()});

I want a div to get an inline style of the image it wraps around.

Pen here: http://codepen.io/stevenmorgan94/pen/xuEwm

HTML

<div class="box">
  <img src="http://placehold.it/250x150" />
</div>

JS

var img = $(".box > img");
$(".box").css({width:img.width(), height:img.height()});

So jquery get image dimensions, add inline style to .box with the width and height

解决方案

jonathan didn't get it right. His solution not about what was asked. Correct one is

var img = $(".box > img");
$(".box").css({width:img.width(), height:img.height()});

这篇关于jQuery获取图像尺寸,应用于Div的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆