Javascript jquery.contents()完成时如何等待?

Javascript jquery.contents()完成时如何等待?,javascript,jquery,iframe,Javascript,Jquery,Iframe,我希望在加载iframe时动态设置其高度。为此,我编写了这段JavaScript代码,在我的梦中它可以做到这一点。但事实上,这是不可能的 这是我的密码 var iframes = (function () { /* jQuery object of an iframe container */ var obj = null, /* Constructor function */ initialize = function (obj) { this.obj = ob

我希望在加载iframe时动态设置其高度。为此,我编写了这段JavaScript代码,在我的梦中它可以做到这一点。但事实上,这是不可能的

这是我的密码

 var iframes = (function () {
/* jQuery object of an iframe container */
var obj = null,

    /* Constructor function */
    initialize = function (obj) {
      this.obj = obj;
    }

initialize.prototype = {
  content: null,

  setContents: function (callback) {
    var self = this;
    this.obj.load(function () {
      self.content = self.obj.contents();
      if (callback) {callback();}
    });
  },

  /* Returns content of iframe
   */
  getContents: function () {
    return this.content;
  },

  setHeight: function () {
    var height = this.content.find('html').height();
    this.obj.css('height', height);
  },

  /*  Changes an element attribute in an iframe
   *  
   *  param selector: String id or class name to find an element
   *  param attrName: String an attribute's name to be changed
   *  param attrVal:  String value that should be applied to the attribute
   */
  changeAttr: function (selector, attrName, attrVal) {
    this.content.find(selector).attr(attrName, attrVal);
  },

  /*  Changes an element styles
   *  
   *  param selector: String id or class name to find an element
   *  param styles:   Object styles to be applied
   */
  changeStyle: function (selector, styles) {
    this.contents.find(selector).css(styles);
  },
};

return initialize;
 }());
我将setHeight方法作为回调函数发送,但jQuery.contents()看起来是异步的,但它没有任何参数,因此无法向jQuery.contents()发送回调

请帮我找到解决办法

已编辑 我不是一名前端开发人员,我对JavaScript的使用经验也很差,但我已经尝试应用最佳实践。所以我使用模块模式创建了一个对象

毕竟我做了

var ifrm = new iframes($('#ifrm'));
ifrm.setContents(setHeight);

它不起作用(

.contents()
不是异步的。
self
setContents()
的一个局部变量,在
setHeight()
函数中不可用。@Juhana哦,我编辑了这个愚蠢的错误,但代码仍然不起作用(很有可能
这个
没有指向您认为它指向的内容。为什么不将
self
作为参数传递给
setHeight()
?我添加了更多的代码,这样您就可以看到完整的图片。这是什么?它是否与所问问题相关:P
$('#iframeId').on("load",function()){
  var height = this.content.find('html').height();
  this.obj.css('height', height);
}