【问题标题】:JQuery : Set iframe height dynamically by content [duplicate]JQuery:按内容动态设置iframe高度[重复]
【发布时间】:2019-05-05 00:08:50
【问题描述】:

我正在尝试找到用于将 iframe 高度设置为内容高度的有效 jquery 脚本。

我发现了这个:

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

Fiddle

但仅适用于旧版本的 jquery。

对 v3.3 有什么想法吗?

【问题讨论】:

  • 否 @Sylent,由于 jQuery 3.x 版本,它无法正常工作。所以它不是重复的。 OP 说它可以使用旧版本的 jquery,当然会。但它不适用于 jQuery v3.x,因为它没有 .load(), .unload(), and .error()functions。
  • 删除评论是对的!

标签: jquery iframe


【解决方案1】:

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

$("#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.

【讨论】:

    【解决方案2】:

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

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

    Here is the working Fiddle

    【讨论】:

      猜你喜欢
      • 2014-01-07
      • 1970-01-01
      • 2013-08-02
      • 2018-06-30
      • 2014-10-19
      • 2012-08-19
      • 1970-01-01
      • 2011-10-24
      • 2015-04-14
      相关资源
      最近更新 更多