2013-03-13 53 views
2

我已经冲刷的interwebs,发现从一个元素的值相当多,涉及到什么我想要做的答案,如:
Javascript - Get element from within an iFrame
How can I access iframe elements with Javascript?
Access iframe elements in JavaScript使用jQuery获得一个iFrame

没有正确的结果。

我目前在iFrame中有一个测试框。从父母,我需要获得文本框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> 

编辑2个 I帧被加载像这样。 (在父)

<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 

其中THEL将页面加载到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> 
+0

它必须完成嘿... – TheGeekZn 2013-03-13 10:30:43

+0

你可以请你的HTML代码或例如jsfiddle吗? – 2013-03-13 10:30:48

+0

我已经添加了代码。 - 使用iFrame。我没有删除任何东西,因为其他控件乱七八糟。 – TheGeekZn 2013-03-13 10:49:07

回答

0

我终于跌跌撞撞翻过的解决方案。

我不得不把代码放在:$(window).load(function() {/*code*/}),毕竟我的网页代码。

只有这样才能工作。

1

线

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

...失败,因为DOM元素不具备的功能对他们所谓的hide。这是一个jQuery的东西。

您可以在jQuery的情况下包裹元素:

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

这会触发第二次提醒,但不会对控件做任何事情。 – TheGeekZn 2013-03-13 10:41:34

+0

@NewAmbition:我们需要更多的上下文来进一步帮助。 – 2013-03-13 10:42:24

+0

我添加了如何加载iFrame – TheGeekZn 2013-03-13 10:47:44