【问题标题】:iFrame Inside a Modal Window Not Working on Chrome模态窗口内的 iFrame 在 Chrome 上不起作用
【发布时间】:2014-12-29 22:07:40
【问题描述】:

这适用于 Firefox,但在 Chrome 上,我在“function onDialogOpen() { this.iframe.css({”) 上收到“Uncaught TypeError: Cannot read property 'css' of null”错误。

  function show(options) {


    // create our temporary iframe
    this.iframe = $('<iframe name="' + (this.id = 'emh_' + +new Date) + '">')
      .load($.proxy(onIframeLoad, this));

    // show our dialog
    this.iframe.dialog($.extend(true, {
      title: 'Entity Viewer',
      modal: true,
      draggable: false,
      resizable: false,
      width: 800,
      height: 500,
      buttons: {
        'Save & Continue': $.proxy(submitIframeForm, this)
      },
      open: $.proxy(onDialogOpen, this),
      close: $.proxy(onDialogClose, this)
    }, options));  
    alert(options.path);
    // since our dialog is showing now, let's update the src of the iframe
    this.iframe.attr('src', options.path + '?modal=true&unique=' + this.id);


  }

function onDialogOpen() {
    this.iframe
      .css({
        width: '100%',
        padding: 0,
        border: 0,
        margin: 0
      });
}

【问题讨论】:

    标签: javascript jquery css google-chrome iframe


    【解决方案1】:

    不确定这是否可行,但我有两种方法:

    1. 在你的 show 方法中移动 onDialogOpen:

       function show(options) {
      
       //your code
       //...
       //...  
       //this.iframe.attr('src', options.path + '?modal=true&unique=' + this.id);
      
      //no need to pass this parameter since method has same this as parent one
      
          function onDialogOpen() {
              this.iframe
                .css({
                  width: '100%',
                  padding: 0,
                  border: 0,
                  margin: 0
                });
          }
      

      }

    2。 - 在 IFrame 加载后立即创建此引用:

    var _this = this;
    
    • 修改此调用:

    打开:$.proxy(function(){ onDialogOpen(_this)}, this)

    更新 onDialogOpen:

    function onDialogOpen(_this) {
        if (typeof _this != 'undefined')
    {
     _this.iframe
          .css({
            width: '100%',
            padding: 0,
            border: 0,
            margin: 0
          });
    }
    else
    {
        this.iframe
          .css({
            width: '100%',
            padding: 0,
            border: 0,
            margin: 0
          });
    }
    }
    

    【讨论】:

    • SilentTremor 感谢您的反馈,但不幸的是我仍然遇到同样的错误
    • jsfiddle.net/pkxjmv9q/1 代码中的一些修复:p 现在应该可以正常工作了。
    猜你喜欢
    • 2015-12-31
    • 1970-01-01
    • 1970-01-01
    • 2012-09-29
    • 2013-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多