【问题标题】:Using jQuery to get a value from an element in an iFrame使用 jQuery 从 iFrame 中的元素获取值
【发布时间】:2013-03-13 10:24:55
【问题描述】:

我搜索了互联网,找到了很多与我想做的事情相关的答案,例如:
Javascript - Get element from within an iFrame
How can I access iframe elements with Javascript?
Access iframe elements in JavaScript

没有正确的结果。

我目前在 iFrame 中有一个测试框。我需要从 Parent 获取文本框 onBlur() 的值。

我目前正在使用;

alert("before");
var iframe = document.getElementById('GeneralInformationPrivate.aspx');
var innerDoc = iframe.contentDocument || iframe.contentWindow.document;

//innerDoc.getElementById("imageLoc").hide();

alert("after");

注释行后,我看到两个警报。如果没有注释,它不会走那么远。
(我把它隐藏起来只是作为一个测试——这不起作用)。

谁能指出我正确的方向。

编辑 加载的 iFrame HTML 如下所示:

<iframe id="GeneralInformationPrivate.aspx" class="iframetab" src="GeneralInformationPrivate.aspx"><!-- Controls Here --></iframe>

Edit 2 iFrame 是这样加载的。 (在父级中)

<div style="float: right; width: 570px;">
            <div id="tabs">
                <ul style="">
                    <li><a class="tabref" href="#tabs-1" rel="GeneralInformationPrivate.aspx">General</a></li>
                    <li><a class="tabref" href="#tabs-2" rel="www.google.co.za">Friends</a></li>
                    <li><a class="tabref" href="#tabs-3" rel="">Challenges</a></li>
                </ul>
                <div id="tabs-1">
                </div>

                <div id="tabs-2">
                </div>

                <div id="tabs-3">
                </div>
            </div>
        </div>

在文档上加载(仍在父级中)

// ++ IFRAME TABS
            var $tabs = $('#tabs').tabs(); //Set the tab

            //Loads the initial tab
            function getSelectedTabIndex() {
                return $tabs.tabs('option', 'selected');
            }
            beginTab = $("#tabs ul li:eq(" + getSelectedTabIndex() + ")").find("a");

            //Send the correct data to load
            loadTabFrame($(beginTab).attr("href"), $(beginTab).attr("rel"));

            // Does something everytime you click the tab
            $("a.tabref").click(function () {
                //event code
            });

            //Load the actual iFrame
            function loadTabFrame(tab, url) {
                if ($(tab).find("iframe").length == 0) {
                    var html = [];
                    html.push('<div class="tabIframeWrapper">');
                    html.push('<iframe id="'+ url +'" class="iframetab" src="' + url + '">Load Failed?</iframe>');
                    html.push('</div>');
                    $(tab).append(html.join(""));
                    //$(tab).find("iframe").height($(window).height() - 80); // Set the height of the iFrame (Currently using CSS - See controlStyle.css iFrame)
                }
                return false;
            }

            //Make sure its only loaded on click
            $("a.tabref").click(function () {
                loadTabFrame($(this).attr("href"), $(this).attr("rel"));
            });
            // -- IFRAME TABS

将页面加载到 iFrame 中。

编辑 3 iFrame 的内容(对不起)

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GeneralInformationPrivate.cs" Inherits="member.Default2" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>General Information</title>
</head>
<body style="background: white">
    <form id="form1" runat="server">
        <div style="min-height: 100px; width: 100%; padding: 0px" id="divProInfo">
                <table style="width: 100%">
                    <tr>
                        <td>
                            <div class="divRegInput" style="padding: 5px 0px 10px 15px">
                                <asp:Label ID="Label12" runat="server" Text="Image URL" Font-Bold="True"></asp:Label>
                                <asp:Label ID="Label13" runat="server" Text="make yourself look good" CssClass="styleLabelWatermarkWashout"></asp:Label>
                                <br />
                                <input id="imageLoc" class="styleTextBoxCenter Larger" />
                            </div>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <div class="divRegInput" style="padding: 5px 0px 10px 15px">
                                <asp:Label ID="Label6" runat="server" Text="Website URL" Font-Bold="True"></asp:Label>
                                <asp:Label ID="Label7" runat="server" Text="show others where you came from" CssClass="styleLabelWatermarkWashout"></asp:Label>
                                <br />
                                <asp:TextBox ID="TextBox1" runat="server" CssClass="styleTextBoxCenter Larger" />
                                <asp:Label ID="lblWebsiteTitle" runat="server" CssClass="styleLabelWatermark"></asp:Label>

                            </div>
                        </td>
                    </tr>
                    <%--<tr>
                        <td>
                            <asp:Button ID="Button1" runat="server" Text="Button" CssClass="styleButtonGreenHolder" Height="43px" CausesValidation="False" OnClick="btnChangeChallenge_Click" Width="195px" />
                        </td>
                    </tr>--%>
                </table>
            </div>
        </div>
    </form>
</body>
</html>

【问题讨论】:

  • 它必须完成嘿...
  • 能否请您在 jsfiddle 上发布您的 html 代码或示例?
  • 我已经添加了代码。 - 使用 iFrame。我没有删除任何东西,只是以防其他控件乱七八糟。

标签: jquery asp.net iframe


【解决方案1】:

线

innerDoc.getElementById("imageLoc").hide();

...失败是因为 DOM 元素上没有名为 hide 的函数。那是一个 jQuery 的东西。

您可以将元素包装在 jQuery 实例中:

$(innerDoc.getElementById("imageLoc")).hide();

【讨论】:

  • 这会触发第二个警报,但不会对控件执行任何操作。
  • @NewAmbition:我们需要更多背景信息来提供进一步帮助。
  • 我添加了加载 iFrame 的方式
  • @NewAmbition:仍然没有告诉我们有关iframe内容 的任何信息,这很重要,因为您说上面的代码没有做任何内容。
  • @NewAmbition:嗯,你显然有一个带有id "imageLoc" 的元素,我不明白为什么上面不会隐藏它......
【解决方案2】:

我终于偶然发现了解决方案。

在我的所有页面代码之后,我必须将代码放入:$(window).load(function () {/*code*/})

只有这样才有效。

【讨论】:

    猜你喜欢
    • 2010-11-08
    • 2013-12-28
    • 1970-01-01
    • 1970-01-01
    • 2017-06-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多