JQuery:按内容动态设置iframe高度

时间:2018-05-08 12:50:00

标签: jquery iframe

我正在尝试找到用于设置iframe高度等于内容高度的工作jquery脚本。

我发现了这个:

$("#IframeId").load(function() {
    $(this).height( $(this).contents().find("html").height() );
});

Fiddle

但仅适用于旧版本的jquery。

v3.3的任何想法?

2 个答案:

答案 0 :(得分:2)

像这样更改你的代码,你会很高兴:

$("#IframeId").on("load",function() {
    $(this).height( $(this).contents().find("html").height() );
});

请注意,我使用的是.on("load")而不是.load()。这是因为:

 .load(), .unload(), and .error() have been removed with jquery 3.x

因此,您需要更改:

$(window).load(function() {});

$(window).on("load", function (e) {});

Here is the working code.

答案 1 :(得分:1)

这是因为jQuery的更新版本使用on API method来附加事件。

$("#IframeId").on("load",function() {
    $(this).height( $(this).contents().find("html").height() );
});

Here is the working Fiddle

相关问题