最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to get actual widthheight of some element after loading whose widthheight originally set by PERCENTAGE? - Stack

programmeradmin0浏览0评论

I have an iframe, whose content points to an HTML document.

In that HTML document, there's an EMBED element pointing to a PDF document. This EMBED originally set up with width and height in PERCENTAGE (such as: width: 100%; height: 100%).

My question is:

After the iframe has been loaded, how can I retrieve the actual, absolute width and height (in any unit px, pt, em etc.) of the EMBED element.

I've been trying on all possible js properties (width, height, style: clientWidth, clientHeight, offsetWidth, offsetHeight...) and in jQuery as well (such as outerWidth, outerHeight etc.) but it seems hopeless.

I have an iframe, whose content points to an HTML document.

In that HTML document, there's an EMBED element pointing to a PDF document. This EMBED originally set up with width and height in PERCENTAGE (such as: width: 100%; height: 100%).

My question is:

After the iframe has been loaded, how can I retrieve the actual, absolute width and height (in any unit px, pt, em etc.) of the EMBED element.

I've been trying on all possible js properties (width, height, style: clientWidth, clientHeight, offsetWidth, offsetHeight...) and in jQuery as well (such as outerWidth, outerHeight etc.) but it seems hopeless.

Share Improve this question edited Jul 20, 2017 at 14:39 Cœur 38.8k25 gold badges206 silver badges278 bronze badges asked Aug 18, 2011 at 7:19 Undefined IdentityUndefined Identity 4953 gold badges7 silver badges17 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3

This should return what you need:

document.getElementById("id").clientHeight 
document.getElementById("id").clientWidth 

id is a unique ID of the EMBED element.

For more about this:

https://developer.mozilla/en/DOM:element.clientHeight

https://developer.mozilla/en/DOM:element.clientWidth

http://jsfiddle/purmou/9XXwf/2/

This uses jQuery's width() and height(). Currently it finds the total width/height of an object, including padding, border, margins, etc. If you'd like to find the width and height of the element itself, excluding the the borders, etc., replace var width = this.offsetWidth; with var width = $(this).width(); and var height = this.offsetHeight; with var height = $(this).height();.

You should use the .offsetWidth and .offsetHeight properties. Note! they belong to the element, not element.style.

var width = document.getElementById('foo').offsetWidth; 

It works in all modern browsers. But beware! offsetWidth/offsetHeight can return 0 if you've done certain DOM modifications to the element recently. You may have to call this code in a setTimeout call after you've modified the element. Maybe that was your issue?

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论