jQuery:嵌套框架集中的访问框架

时间:2022-04-14 13:38:03

I have a document which has a nested frameset. I need to access one of the nested frames, named "sq_main", and access content inside this frame. Here is my structure:

我有一个具有嵌套框架集的文档。我需要访问其中一个名为“sq_main”的嵌套框架,并访问此框架内的内容。这是我的结构:

<html>
<head>
<title>Title</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<frameset rows="28,*" frameborder="0" border="0">
  <frame src="/_admin/?SQ_BACKEND_PAGE=header" name="sq_header" scrolling="no" marginwidth="0" marginheight="0">
  <frameset cols="380,10,*" frameborder="0" border="0" id ="main_frameset">
    <frame src="/_admin/?SQ_BACKEND_PAGE=sidenav" name="sq_sidenav" scrolling="no" marginwidth="0" marginheight="0">
    <frame src="/_admin/?SQ_BACKEND_PAGE=resizer" name="sq_resizer" scrolling="no" marginwidth="0" marginheight="0">
    <frame src="/_admin?SQ_BACKEND_PAGE=main&assetid=43&sq_from_frontend=1" name="sq_main" marginwidth="0" marginheight="0" scrolling="yes">
  </frameset>
</frameset>
<noframes></noframes>
</html>

Sadly, I can't change the code, that is why I need to access it with jQuery. I have tried to write a jQuery selector to access the "sq_main" frame, but no luck so far:

遗憾的是,我无法更改代码,这就是我需要使用jQuery访问它的原因。我试图编写一个jQuery选择器来访问“sq_main”框架,但到目前为止没有运气:

$('body', parent.frames[0].sq_main).prepend('<h1>TEST!!!!</h1>');

Ideas on how to drill down into this ugly structure? :)

关于如何深入挖掘这个丑陋结构的想法? :)

3 个解决方案

#1


9  

Try to navigate one step at a time. IIRC, the frames array only works with iframes. Try the selector frame[name = 'sq_main'] instead.

尝试一次导航一步。 IIRC,帧阵列仅适用于iframe。请尝试使用选择器框[name ='sq_main']。

Example by Ronny Sherer:

Ronny Sherer的例子:

var frameDocument = $('frame[name="mainFrame"]', top.document)[0].contentDocument;
$(frameDocument).find('body').prepend('<h1>TEST!!!!</h1>');

#2


4  

var sql_mainJQ = $("frame[name='sql_main']", top.document);

//$('body', sql_mainJQ.contents()).prepend("TEST!!!!"); // :( Bad

var frameContent = sql_mainJQ[0].contentDocument;
if ($.browser.msie) {
    frameContent = mainFrameJQ[0].contentWindow.document;
} else {
    frameContent = mainFrameJQ[0].contentDocument;
}
$('body', sql_mainJQ.contents()).prepend("TEST!!!!"); // :> Maybe OK!

#3


0  

<html>
<head>
    <title>frames.html</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<frameset rows="100,*" frameborder="1" border="2">  
                <frame src="helloworld.html" name="sq_header" id="sq_header" scrolling="yes" marginwidth="0" marginheight="0">
                   <frameset cols="380,300,*" frameborder="1" border="2" id ="main_frameset">  
                   <frame src="helloworld.html" name="sq_sidenav"  id="sq_sidenav" scrolling="yes" marginwidth="0" marginheight="0"> 
                   <frame src="anotherpage.html" name="sq_resizer" id="sq_resizer" scrolling="yes" marginwidth="0" marginheight="0">   
                   <frame src="helloworld.html" name="sq_main" id="sq_main" marginwidth="0" marginheight="0" scrolling="yes">  
                   </frameset> 
                   </frameset>
<noframes>
</noframes>
</html>
<html>
<head>
    <title>anotherpage.html</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">

    <script type="text/javascript" src="http://www.google.com/jsapi"></script>

    <script type="text/javascript">
                  //http://encosia.com/3-reasons-why-you-should-let-google-host-jquery-for-you/
                  // You may specify partial version numbers, such as "1" or "1.3",
                  //  with the same result. Doing so will automatically load the 
                  //  latest version matching that partial revision pattern 
                  //  (e.g. 1.3 would load 1.3.2 today and 1 would load 1.7.2).
                  google.load("jquery", "1.6.2");

                  google.setOnLoadCallback(function() {
                    // Place init code here instead of $(document).ready()
                  });
    </script>

    <script language="Javascript">
                var d = new Date();
                var n = d.getTime(); 

                $(document).ready(function(){  
                   $('#title').html($("title").html());
/*
this is to work in safari
see "Updated answer provided below....looks like a setTimeout maybe needed as the     frames aren't loaded when the initial startup script runs. – David Hoerster Aug 21 '10 at 16:38
url: http://*.com/questions/3534082/jquery-access-table-inside-a-frame
*/
setTimeout(writemsg, 2000);
function writemsg() {
                   $('#helloworld',top.frames["sq_main"].document).html("Write from "+ $("title").html()+" in sq_main at "+ n);
}
                  }); 

    </script>

</head>
<body>
    <div id="title">
    </div>
    </p>
    <div id="helloworld">
        Hello World JQuery!</div>
</body>
</html>
<html>
<head>
    <title>helloworld.html</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">

    <script type="text/javascript" src="http://www.google.com/jsapi"></script>

    <script type="text/javascript">
                  //http://encosia.com/3-reasons-why-you-should-let-google-host-jquery-for-you/
                  // You may specify partial version numbers, such as "1" or "1.3",
                  //  with the same result. Doing so will automatically load the 
                  //  latest version matching that partial revision pattern 
                  //  (e.g. 1.3 would load 1.3.2 today and 1 would load 1.7.2).
                  google.load("jquery", "1.6.2");

                  google.setOnLoadCallback(function() {
                    // Place init code here instead of $(document).ready()
                  });
    </script>

    <script language="Javascript">
                $(document).ready(function(){  
                   $('#title').html($("title").html());
                  }); 

    </script>

</head>
<body>
    <div id="title">
    </div>
    </p>
    <div id="helloworld">
        Hello World JQuery!</div>
</body>
</html>

#1


9  

Try to navigate one step at a time. IIRC, the frames array only works with iframes. Try the selector frame[name = 'sq_main'] instead.

尝试一次导航一步。 IIRC,帧阵列仅适用于iframe。请尝试使用选择器框[name ='sq_main']。

Example by Ronny Sherer:

Ronny Sherer的例子:

var frameDocument = $('frame[name="mainFrame"]', top.document)[0].contentDocument;
$(frameDocument).find('body').prepend('<h1>TEST!!!!</h1>');

#2


4  

var sql_mainJQ = $("frame[name='sql_main']", top.document);

//$('body', sql_mainJQ.contents()).prepend("TEST!!!!"); // :( Bad

var frameContent = sql_mainJQ[0].contentDocument;
if ($.browser.msie) {
    frameContent = mainFrameJQ[0].contentWindow.document;
} else {
    frameContent = mainFrameJQ[0].contentDocument;
}
$('body', sql_mainJQ.contents()).prepend("TEST!!!!"); // :> Maybe OK!

#3


0  

<html>
<head>
    <title>frames.html</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<frameset rows="100,*" frameborder="1" border="2">  
                <frame src="helloworld.html" name="sq_header" id="sq_header" scrolling="yes" marginwidth="0" marginheight="0">
                   <frameset cols="380,300,*" frameborder="1" border="2" id ="main_frameset">  
                   <frame src="helloworld.html" name="sq_sidenav"  id="sq_sidenav" scrolling="yes" marginwidth="0" marginheight="0"> 
                   <frame src="anotherpage.html" name="sq_resizer" id="sq_resizer" scrolling="yes" marginwidth="0" marginheight="0">   
                   <frame src="helloworld.html" name="sq_main" id="sq_main" marginwidth="0" marginheight="0" scrolling="yes">  
                   </frameset> 
                   </frameset>
<noframes>
</noframes>
</html>
<html>
<head>
    <title>anotherpage.html</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">

    <script type="text/javascript" src="http://www.google.com/jsapi"></script>

    <script type="text/javascript">
                  //http://encosia.com/3-reasons-why-you-should-let-google-host-jquery-for-you/
                  // You may specify partial version numbers, such as "1" or "1.3",
                  //  with the same result. Doing so will automatically load the 
                  //  latest version matching that partial revision pattern 
                  //  (e.g. 1.3 would load 1.3.2 today and 1 would load 1.7.2).
                  google.load("jquery", "1.6.2");

                  google.setOnLoadCallback(function() {
                    // Place init code here instead of $(document).ready()
                  });
    </script>

    <script language="Javascript">
                var d = new Date();
                var n = d.getTime(); 

                $(document).ready(function(){  
                   $('#title').html($("title").html());
/*
this is to work in safari
see "Updated answer provided below....looks like a setTimeout maybe needed as the     frames aren't loaded when the initial startup script runs. – David Hoerster Aug 21 '10 at 16:38
url: http://*.com/questions/3534082/jquery-access-table-inside-a-frame
*/
setTimeout(writemsg, 2000);
function writemsg() {
                   $('#helloworld',top.frames["sq_main"].document).html("Write from "+ $("title").html()+" in sq_main at "+ n);
}
                  }); 

    </script>

</head>
<body>
    <div id="title">
    </div>
    </p>
    <div id="helloworld">
        Hello World JQuery!</div>
</body>
</html>
<html>
<head>
    <title>helloworld.html</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">

    <script type="text/javascript" src="http://www.google.com/jsapi"></script>

    <script type="text/javascript">
                  //http://encosia.com/3-reasons-why-you-should-let-google-host-jquery-for-you/
                  // You may specify partial version numbers, such as "1" or "1.3",
                  //  with the same result. Doing so will automatically load the 
                  //  latest version matching that partial revision pattern 
                  //  (e.g. 1.3 would load 1.3.2 today and 1 would load 1.7.2).
                  google.load("jquery", "1.6.2");

                  google.setOnLoadCallback(function() {
                    // Place init code here instead of $(document).ready()
                  });
    </script>

    <script language="Javascript">
                $(document).ready(function(){  
                   $('#title').html($("title").html());
                  }); 

    </script>

</head>
<body>
    <div id="title">
    </div>
    </p>
    <div id="helloworld">
        Hello World JQuery!</div>
</body>
</html>