【问题标题】:Iframe jquery event handlers not firing when redirected from parent frame从父框架重定向时,iframe jquery 事件处理程序未触发
【发布时间】:2014-06-16 21:45:49
【问题描述】:

我们的解决方案使用父框架和主要内容 iframe 运行。主要内容 iframe 中的一个页面设置了一系列用作选项卡的 div,示例如下。

<div id="topRow">
    <div class="mainInformation tab tabSelected" runat="server" id="mainInformationTop">
        <div class="tabLeft">
            <div class="tabMain">
                Main Information
            </div>
        </div>
    </div>
    <div class="executives tab" runat="server" id="executivesTop">
        <div class="tabLeft">
            <div class="tabMain">
                Executives
            </div>
        </div>
    </div>                
</div
<div id="secondRow">
    <div id="mainInformationTabGroup" runat="server" class="mainInformation tabGroup">
        <div id="overview" runat="server" class="tab tabSelected overview">
            <div class="tabLeft">
                <div class="tabMain">
                    Overview
                </div>
            </div>
        </div>
        <div id="locations" runat="server" class="tab locations">
            <div class="tabLeft">
                <div class="tabMain">
                    Locations
                </div>
            </div>
        </div>
    </div>
    <div id="executivesTabGroup" runat="server" class="executives tabGroup" style="display: none">
        <div id="companyExecutives" runat="server" class="tab companyExecutives">
            <div class="tabLeft">
                <div class="tabMain">
                    Company Executives
                </div>
            </div>
        </div>
        <div id="affiliatedExecutives" runat="server" class="tab affiliatedExecutives">
            <div class="tabLeft">
                <div class="tabMain">
                    Affiliated Company Executives
                </div>
            </div>
        </div>
    </div
</div>

jquery 事件处理程序是从本地 .js 文件加载的,与上述 HTML 相关的示例如下

$('#topRow div.tab').click(function() {
    $('#topRow div.tabSelected').removeClass('tabSelected');
    $(this).addClass('tabSelected');
})
$('#topRow .mainInformation').click(function() {
    $('#secondRow').css('display', 'block');
    $('#thirdRow').css('display', 'none');
    resizeAllIframes();
    $('#secondRow .tabGroup').css('display', 'none');
    $('#secondRow .mainInformation').css('display', 'block');
    $('#secondRow .mainInformation div.tab').not('.locked').children().first().trigger('click');
});
$('#topRow .executives').click(function(){
    $('#secondRow').css('display', 'block');
    $('#thirdRow').css('display', 'none');
    resizeAllIframes();
    $('#secondRow .tabGroup').css('display', 'none');
    $('#secondRow .executives').css('display', 'block');
    $('#secondRow .executives div.tab').not('.locked').children().first().trigger('click');
});

(点击事件只是改变了 iframe 的“src”属性)

我遇到的问题是,有时当我们加载这个标签页时(而且只有在 Internet Explorer 9+ 中,IE7 似乎没有这个问题),我会点击一个标签,但什么也不会发生。 CSS 的悬停事件加载得很好,但单击选项卡没有任何作用。但是,如果我单击一个选项卡但没有任何反应,我可以单击下一个选项卡,它会正常工作。同样,如果我先单击主内容框架中的任意位置,然后单击选项卡,它总是会触发事件处理程序。

当我们尝试从父框架(我们在那里有一个快速搜索)并使用 Response.Redirect 加载带有标签页的 iframe 时,我们几乎完全注意到了这个问题。我尝试将此快速搜索重定向到 iframe 内的登录页面,然后使用 Response.Redirect 将我们带到标签页,但最终得到相同的结果。当我们从 iframe (Target="_self") 内部单击超链接时,效果很好。

在 Firefox、Chrome 或 IE7 中不会出现此问题,但在 IE9+ 中会出现。

有没有人有任何想法、想法、我遗漏的已知错误,诸如此类?

【问题讨论】:

  • 你用的是什么版本的jQuery?
  • 它是 1.4.2 - 令人痛苦地过时了,但我们暂时坚持使用它。
  • 这很奇怪,但我猜它必须与此有关!但恐怕我从未见过这样的问题! :-s
  • 我担心这也是问题所在。这是一个令人头疼的问题,这是肯定的!
  • 尝试将点击事件绑定到元素,看看能不能解决问题!

标签: c# jquery iframe internet-explorer-7 internet-explorer-9


【解决方案1】:

这是将事件绑定到元素的方式:

$(document).bind('click','#topRow div.tab',function() {
    $('#topRow div.tabSelected').removeClass('tabSelected');
    $(this).addClass('tabSelected');
});

对其他事件执行完全相同的操作,只需使用所需的选择器更改选择器部分(我的意思是“#topRow div.tab”)。

【讨论】:

  • 好的,我试试看。我担心的是我在原始帖子中提到的,除了我们 Response.Redirect() iframe 的情况之外,这些事件通常都很好,并且在您单击 iframe 内的其他位置之前它们不起作用。让我看看显式绑定是否有帮助。
  • 好吧,在外部 .js 文件中尝试了一些显式绑定并将函数实际放入 Tab 页本身之后,问题仍然存在。我们将探索升级我们的 jQuery,也许我们只需要加快速度。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-15
相关资源
最近更新 更多