开发者

jquery div contents real width

开发者 https://www.devze.com 2022-12-20 02:00 出处:网络
i have a div tag <div id=\"openfrag\"> <script language=\"JavaScript\" src=\"http://ad.uk.doubleclick.net/adj/BA_HOME/HP_Public_MSB; dcopt=ist;sz=728x90,468x60;tile=1;lan=en;ord=1265795605?

i have a div tag

<div id="openfrag">
 <script language="JavaScript" src="http://ad.uk.doubleclick.net/adj/BA_HOME/HP_Public_MSB; dcopt=ist;sz=728x90,468x60;tile=1;lan=en;ord=1265795605?" type="text/javascript"></script>
</div>

above script tag is third party url that may returns either image or flash object or any other of any size.. how to determine dynamic width and height occupied by div tag using jquery ?

i tried $("#openxfrag").width() ,but it always return the same width that is define开发者_如何学God in css


Check the width after the content has been loaded into it. You can check the width, or the outerWidth:

$("#openfrag").width();
$("#openfrag").outerWidth();

outerWidth includes padding and borders.

If you're setting a default width with CSS, you may consider removing that (as it overrides the natural width) or replacing it with min-width instead (assuming the future width will be larger).


i tried $("#openxfrag").width() ,but it always return the same width that is defined in css

Why wouldn't it? If you're assigning it a width in the CSS, then that's the width it'll have! If you want its width to be dictated by its contents, you'll have to avoid specifying a width... and you'll have to make it shrink-wrap the contents.

Only then can you expect $("#openfrag").width() to do something useful.


The simplest way to do this is to create a new, hidden div and then check its width. It will be the size of its contents because it has no CSS defined and will expand to fit its content.

HTML:

<div id="getWidth" style="display: none;">
   <!-- Put contents here -->
</div>

JavaScript:

<script type="text/javascript">
   console.log($("#getWidth").width());
</script>
0

精彩评论

暂无评论...
验证码 换一张
取 消