【问题标题】:Access <select> in child iFrame from MasterPage (in same domain)从 MasterPage 访问子 iFrame 中的 <select>(在同一域中)
【发布时间】:2011-05-10 06:41:34
【问题描述】:

在我的 ASP.NET 应用程序中,我使用 MasterPage 和 iFrame 作为应用程序的内容部分(iFrame 之外还有一个侧边栏和菜单栏)。

我的目标是插入一个 jQuery/javascript 函数来拦截 &lt;select&gt; 元素在子 iFrame 中获得焦点。

MasterPage 和 iFrame 都是同一个应用程序的一部分,iFrame 不打开外部页面,而是打开应用程序的 .aspx 页面,因此是同一个域。

但无论如何,“焦点”事件似乎不起作用,因为内部代码没有执行。

函数如下:

$("#tabFrame").load(function() {
    $("#tabFrame").contents().find("SELECT").focus(function() {
        alert('focus on');
    }).change(function() {
        $.ajax({
            type: "POST",
            url: "../WebService/registerChanges",
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            data: JSON.stringify({
                controlName: dropDownId
            }),
            success: function(data) {
                var result = JSON.parse(data.d);
                if (result.Success) {
                    alert("Successfully found.");
                }
            }
        })
    })
})

由于函数本身是正确的,我想尝试从外部访问内部 IFrame 存在一些问题,但我不知道是哪些问题。

有什么建议吗? (我用IE8作为浏览器)

谢谢。

【问题讨论】:

  • 您是否确认 $("#tabFrame").load(...) 正在触发?
  • 因为您在 $("#tabFrame").load(..) 中,可能 $("#tabFrame") 不匹配任何内容 - 尝试更改 $("#tabFrame" ).contents().find("select") 到 $("select")
  • 为了验证 iframe 是否被触发,我在 $("#tabFrame").load() 函数之后放置了一个简单的警报。一开始我只是使用 $("select"),但即使那样它也不起作用
  • 尝试运行它来告诉你内部选择是否匹配任何 dom 元素: $("#tabFrame").load(function() { alert($("#tabFrame").contents( ).find("SELECT").length); });

标签: jquery asp.net iframe


【解决方案1】:

现在可以了:

  • 最初的问题是由于 IFrame,因为我试图在加载之前访问它。使用 $("#tabFrame").load() 函数可以在正确的时间访问它。

  • 之后,缺少 jQuery 引用 (JSON) 是问题的根源 :( 但是落后一步并简单地发出警报而不是 WebService 调用,我可以做到这一点。

上面的代码是工作版本。

【讨论】:

  • @Natan:感谢您的提示,他们让我朝着正确的方向前进。
  • 我的荣幸 - 如果 cmets 有用,请记得给他们投票 :)
  • @Natan:我看到可以为答案投票,但我如何投票给 cmets?
猜你喜欢
  • 2010-10-16
  • 1970-01-01
  • 2012-10-31
  • 1970-01-01
  • 2014-05-20
  • 1970-01-01
  • 1970-01-01
  • 2016-11-23
  • 1970-01-01
相关资源
最近更新 更多