javascript - 模态窗口内的 iFrame 在 Chrome 上不起作用

标签 javascript jquery css google-chrome iframe

这在 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
      });
}

最佳答案

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

  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
      });
}
}

关于javascript - 模态窗口内的 iFrame 在 Chrome 上不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26711069/

相关文章:

Javascript 正则表达式替换第 6 次出现的 ; (分号)与 ;\n(分号和换行符)

javascript - slider 中不显示图像

javascript - 从 UI/UX 开发中抛弃 jQuery

javascript - 从 jquery 对话框刷新父页面

html - 如何在中心垂直挤压一个div?

javascript - 我需要 swfobject.js 的替代解决方案

javascript - 按日期过滤的正确方法是什么

javascript - 传单 geojson 样式函数功能仅颜色 1 geojson 功能

javascript - jQuery有这样的效果吗?这是使用原型(prototype)

html - 样式自定义 chrome 扩展