如何在iframe中选择div #id(来自插件方法)

时间:2012-03-27 16:21:20

标签: javascript jquery iframe jquery-selectors lightbox

我正在尝试根据其内容制作更大的灯箱,我只需要选择div#id和if length> 0然后我知道我需要把箱子做得更大:

这是插件的方法,它实际上将iframe HTML附加到灯箱窗口中(div #id在iframe内容中)

  appendhtml: function(obj, width, height, mode) {
    if (typeof mode !== 'undefined') {
      this.changemode(mode);
    }

    this.resize(width + 30, height + 20);

    this.esqueleto.background.bind('complete', $.proxy(function() {
      this.esqueleto.background.removeClass(this.options.name+'-loading');
      this.esqueleto.html.append(obj);
      console.log('19:14');
      console.log(obj);
      console.log(obj.contents().find('#bookingEngine').html());
      console.log(obj.find('#bookingEngine').html());
      console.log($(obj).html());
      console.log($(obj).contents().find('#bookingEngine').html());  /*All my hopes where here*/
      console.log($('#bookingEngine').length);
      console.log(this.esqueleto.html.find('iframe').length);
      console.log(this.esqueleto.html.find('iframe').contents().find('#bookingEngine').length);
    }, this));
  } 

这就是我的console.log LOG

19:14 
jquery...tbox.js (línea 679)[iframe#IF_1332866297227]
jquery...tbox.js (línea 680) null
jquery...tbox.js (línea 681) null
jquery...tbox.js (línea 682) (an empty string)
jquery...tbox.js (línea 683) null
jquery...tbox.js (línea 684) 0
jquery...tbox.js (línea 685) 1
jquery...tbox.js (línea 686) 0

我怎样才能到达$('#bookingEngine')?它在iframe里面(jquery ... tbox.js(línea685)1)我能够选择

1 个答案:

答案 0 :(得分:1)

您的解决方案是正确的,我认为问题可能是iframe位于不同的域上,因此您无法控制它。阅读here

这是来自contents()

的文档
  

.contents()方法也可用于获取内容文档   iframe,如果iframe与主页位于同一个域中。

相关问题