简体   繁体   English

如果高度为 150 像素,则隐藏 iframe

[英]Hide iframe if height is 150px

I have an iframe which is loading external content dynamically from a site I have control of.我有一个 iframe,它从我控制的网站动态加载外部内容。

In case no content is available I load an empty page.如果没有可用的内容,我会加载一个空页面。

Unfortunately an iframe has a height of 150px.不幸的是,iframe 的高度为 150 像素。

How can I at best with Javascript define to hide the mother div (container) or iframe in case the iframe has the standard size of 150px?如果 iframe 的标准大小为 150px,我如何最好地使用 Javascript 定义隐藏母 div(容器)或 iframe?

<div id="container">

<iframe src=""> </iframe>

</div>

You can use the client.Height to get the Height of an element (with padding, the border is not included)您可以使用client.Height来获取元素的高度(有填充,不包括边框)

Try this:尝试这个:

const iframe = document.querySelector('iframe');
let iframeHeight = iframe.clientHeight;

if (iframeHeight == 150) {
    iframe.style.display = "none";
}

It will be hide the first iframe with standard height.它将隐藏第一个具有标准高度的 iframe。 Let me know for more requirements.让我知道更多要求。 Hope it helps!希望能帮助到你!

You can use您可以使用

window.getComputedStyle(element);

And it will give you an object with all css properties of that element.它将为您提供一个具有该元素所有 css 属性的对象。

If you want to get the height:如果你想得到高度:

window.getComputedStyle(element).height;

And that will return '150px', or whatever else the height will be这将返回“150px”,或者其他任何高度

Note: typing 'window' is not required, getComputedStyle() will also work注意:不需要输入“window”,getComputedStyle() 也可以

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM