【问题标题】:Do something with ajax-loaded iframe content?用 ajax 加载的 iframe 内容做点什么?
【发布时间】:2012-04-08 16:25:11
【问题描述】:

我通过 fancybox 将一些内容(简单的网络表单)加载到 iframe(来自我的域)。并希望在此 iframe 表单中为一个输入添加值。

试试这个:

$x("a.load_html_i").fancybox({
'type'              :'iframe',
'padding'           : 20,
'width'             : 690,
'height'            : 550,
'onComplete'        : function() {
$x('#fancybox-frame input[name=input_text_0]').val($x(this).attr('title'));
}
});

#fancybox-frame 是 iframe 的 ID。

还有这个:

...
$x("#fancybox-frame").contents().find("input[name=input_text_0]").val($x(this).attr('title'));
...

但它不起作用。

【问题讨论】:

  • 您通过 iframe 加载的页面是否与使用 fancybox 发出请求的页面位于同一域中?
  • 你有我可以看到的工作示例吗?
  • “this”可能与上下文无关。您是否在“this”上执行了 console.log?

标签: jquery ajax iframe


【解决方案1】:

我发现了一个问题:jquery 在 iframe 加载之前运行。如何改进?

$x("iframe#fancybox-frame").ready(function(){
                $x("iframe#fancybox-frame").contents().find("input[name=input_text_0]").val('hh');
            });

添加准备好无济于事

【讨论】:

    【解决方案2】:

    不要使用“this”关键字,而是通过类名引用锚点。我相信 'this' 引用了 fancybox 实例化。

    $x("a.load_html_i").fancybox({
    'type'              :'iframe',
    'padding'           : 20,
    'width'             : 690,
    'height'            : 550,
    'onComplete'        : function() {
    //cache the anchor
    var anchor = $x(this);
    //trigger the following function in 0.5s (should be long enough)
       setTimeout(function(){
          $x('#fancybox-frame input[name=input_text_0]').val(anchor.attr('title'));
       }, 500);
    }
    });
    

    【讨论】:

    • 考虑到它在 onComplete 回调函数中,这令人惊讶……如果是这种情况,只需在 onComplete 回调中设置超时。见上文。
    猜你喜欢
    • 1970-01-01
    • 2011-03-30
    • 2023-03-09
    • 1970-01-01
    • 2012-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多