如何在加载其宽度/高度最初由PERCENTAGE设置后获取某些元素的实际宽度/高度?

时间:2021-01-20 20:30:10

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

我有一个iframe,其内容指向HTML文档。

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%).

在该HTML文档中,有一个指向PDF文档的EMBED元素。这个EMBED最初设置的宽度和高度为PERCENTAGE(例如:宽度:100%;高度: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.

加载iframe后,如何检索EMBED元素的实际绝对宽度和高度(以任何单位px,pt,em等)。

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.

我一直在尝试所有可能的js属性(宽度,高度,样式:clientWidth,clientHeight,offsetWidth,offsetHeight ...)以及jQuery(例如outerWidth,outerHeight等)但它似乎没有希望。

3 个解决方案

#1


3  

This should return what you need:

这应该返回你需要的东西:

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

id is a unique ID of the EMBED element.

id是EMBED元素的唯一ID。

For more about this:

有关这方面的更多信息

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

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

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

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

#2


0  

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

http://jsfiddle.net/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();.

这使用jQuery的width()和height()。目前它找到一个对象的总宽度/高度,包括填充,边框,边距等。如果您想要找到元素本身的宽度和高度,不包括边框等,请替换var width = this .offsetWidth; var var = $(this).width();和var height = this.offsetHeight; var height = $(this).height();.

#3


0  

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

您应该使用.offsetWidth和.offsetHeight属性。注意!它们属于元素,而不是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?

它适用于所有现代浏览器。但要小心!如果您最近对元素进行了某些DOM修改,则offsetWidth / offsetHeight可以返回0。在修改元素后,可能必须在setTimeout调用中调用此代码。也许那是你的问题?

#1


3  

This should return what you need:

这应该返回你需要的东西:

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

id is a unique ID of the EMBED element.

id是EMBED元素的唯一ID。

For more about this:

有关这方面的更多信息

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

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

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

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

#2


0  

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

http://jsfiddle.net/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();.

这使用jQuery的width()和height()。目前它找到一个对象的总宽度/高度,包括填充,边框,边距等。如果您想要找到元素本身的宽度和高度,不包括边框等,请替换var width = this .offsetWidth; var var = $(this).width();和var height = this.offsetHeight; var height = $(this).height();.

#3


0  

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

您应该使用.offsetWidth和.offsetHeight属性。注意!它们属于元素,而不是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?

它适用于所有现代浏览器。但要小心!如果您最近对元素进行了某些DOM修改,则offsetWidth / offsetHeight可以返回0。在修改元素后,可能必须在setTimeout调用中调用此代码。也许那是你的问题?