无法从父级滚动到iframe内的div

时间:2016-10-12 15:28:09

标签: javascript jquery html

我可以使用以下jquery从父html触发对accordion(实现为div)的点击:

jQuery(iframe).contents().find('#XYZ').trigger( "click" );

手风琴正在打开,但我的问题是打开的手风琴被隐藏在下面某处并且在屏幕上不可见。

有人可以建议如何从父窗口滚动到iFrame中的div吗?父母和孩子都在同一个域中。我尝试了.scrollTo.focus,但没有得到正面结果。

2 个答案:

答案 0 :(得分:0)

您可以尝试在iframe网址中设置哈希

jQuery(iframe)[0].contentWindow.location.hash='XYZ';

答案 1 :(得分:0)

我可以帮助你一个手风琴一旦打开就调整自己的方法,但我不会重新创建你的布局来测试它是否有效。

  1. 因为iframe很难创建一个工作示例。
  2. 虽然我确实重新创建了有效的iframe环境,但是你没有提供足够的代码来让它值得我这么做。
  3. 无论如何,您的环境中有太多因素需要考虑。
  4. 我提供的内容在正常情况下正常工作,它未在您描述的环境下进行测试。话虽如此,下面的片段演示了手风琴如何在打开后自动调整它的位置。

    注意:如果手风琴位于页面的最底部且iframe位于最顶部,则可能需要在手风琴下方留出更多垂直空间,以允许其到达iframe的视图。 <footer>将是完美的。

    footer {
      height: 100px;
    }
    <!doctype html>
    <html>
    
    <head>
      <meta charset="utf-8">
      <title>CHILD</title>
      <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
      <link href="https://code.jquery.com/ui/1.11.4/themes/dark-hive/jquery-ui.css" rel="stylesheet" type="text/css">
    </head>
    
    <body>
      <header>
        <h1>TOP of CHILD PAGE</h1>
      </header>
      <hr/>
      <main>
        <section>
          <p>Far far away, behind the word mountains, far from the countries Vokalia and Consonantia, there live the blind texts. Separated they live in Bookmarksgrove right at the coast of the Semantics, a large language ocean. A small river named Duden flows
            by their place and supplies it with the necessary regelialia. It is a paradisematic country, in which roasted parts of sentences fly into your mouth. Even the all-powerful Pointing has no control about the blind texts it is an almost unorthographic
            life One day however a small line of blind text by the name of Lorem Ipsum decided to leave for the far World of Grammar. The Big Oxmox advised her not to do so, because there were thousands of bad Commas, wild Question Marks and devious Semikoli,
            but the Little Blind Text didn’t listen. She packed her seven versalia, put her initial into the belt and made herself on the way. When she reached the first hills of the Italic Mountains, she had a last view back on the skyline of her hometown
            Bookmarksgrove, the headline of Alphabet Village and the subline of her own road, the Line Lane. Pityful a rethoric question ran over her cheek, then she continued her way.</p>
    
          <p>On her way she met a copy. The copy warned the Little Blind Text, that where it came from it would have been rewritten a thousand times and everything that was left from its origin would be the word "and" and the Little Blind Text should turn around
            and return to its own, safe country. But nothing the copy said could convince her and so it didn’t take long until a few insidious Copy Writers ambushed her, made her drunk with Longe and Parole and dragged her into their agency, where they abused
            her for their projects again and again. And if she hasn’t been rewritten, then they are still using her. Far far away, behind the word mountains, far from the countries Vokalia and Consonantia, there live the blind texts. Separated they live in
            Bookmarksgrove right at the coast of the Semantics, a large language ocean. A small river named Duden flows by their place and supplies it with the necessary regelialia. It is a paradisematic country, in which roasted parts of sentences fly into
            your mouth.</p>
    
          <p>Even the all-powerful Pointing has no control about the blind texts it is an almost unorthographic life One day however a small line of blind text by the name of Lorem Ipsum decided to leave for the far World of Grammar. The Big Oxmox advised her
            not to do so, because there were thousands of bad Commas, wild Question Marks and devious Semikoli, but the Little Blind Text didn’t listen. She packed her seven versalia, put her initial into the belt and made herself on the way. When she reached
            the first hills of the Italic Mountains, she had a last view back on the skyline of her hometown Bookmarksgrove, the headline of Alphabet Village and the subline of her own road, the Line Lane. Pityful a rethoric question ran over her cheek, then
            she continued her way. On her way she met a copy. The copy warned the Little Blind Text, that where it came from it would have been rewritten a thousand times and everything that was left from its origin would be the word "and" and the Little
            Blind Text should turn around and return to its own, safe country. But nothing the copy said could convince her and so it didn’t take long until a few insidious Copy Writers ambushed her, made her drunk with Longe and Parole and dragged her into
            their agency, where they abused her for their projects again and again.</p>
    
          <p>And if she hasn’t been rewritten, then they are still using her. Far far away, behind the word mountains, far from the countries Vokalia and Consonantia, there live the blind texts. Separated they live in Bookmarksgrove right at the coast of the
            Semantics, a large language ocean. A small river named Duden flows by their place and supplies it with the necessary regelialia. It is a paradisematic country, in which roasted parts of sentences fly into your mouth. Even the all-powerful Pointing
            has no control about the blind texts it is an almost unorthographic life One day however a small line of blind text by the name of Lorem Ipsum decided to leave for the far World of Grammar. The Big Oxmox advised her not to do so, because there
            were thousands of bad Commas, wild Question Marks and devious Semikoli, but the Little Blind Text didn’t listen. She packed her seven versalia, put her initial into the belt and made herself on the way. When she reached the first hills of the
            Italic Mountains, she had a last view back on the skyline of her hometown Bookmarksgrove, the headline of Alphabet Village and the subline of her own road, the Line Lane. Pityful a rethoric question ran over her cheek, then she continued her way.
            On her way she met a copy. The copy warned the Little Blind Text, that where it came from it would have been rewritten a thousand times and everything that was left from its origin would be the word "and" and the Little Blind Text should turn
            around and return to its own, safe country. But nothing the copy said could convince her and so it didn’t take long until a few insidious Copy Writers ambushed her, made her drunk with Longe and Parole and dragged her into their agency, where
            they abused her for their projects again and again. And if she hasn’t been rewritten, then they are still using her. Far far away, behind the word mountains, far from the countries Vokalia and Consonantia, there live the blind texts. Separated
            they live in Bookmarksgrove right at the coast of the Semantics, a large language ocean. A small river named Duden flows by their place and supplies it with the necessary regelialia. It</p>
        </section>
        <section>
          <div class="accordion">
            <h4>Accordion</h4>
            <div class="frame">
              <div class="content">
                <p>Far far away, behind the word mountains, far from the countries Vokalia and Consonantia, there live the blind texts. Separated they live in Bookmarksgrove right at the coast of the Semantics, a large language ocean. A small river named Duden
                  flows by their place and supplies it with the necessary regelialia. It is a paradisematic country, in which roasted parts of sentences fly into your mouth. Even the all-powerful Pointing has no control about the blind texts it is an almost
                  unorthographic life One day however a small line of blind text by the name of Lorem Ipsum decided to leave for the far World of Grammar. The Big Oxmox advised her not to do so, because there were thousands of bad Commas, wild Question Marks
                  and devious Semikoli, but the Little Blind Text didn’t listen. She packed her seven versalia, put her initial into the belt and made herself on the way. When she reached the first hills of the Italic Mountains, she had a last view back on
                  the skyline of her hometown Bookmarksgrove, the headline of Alphabet Village and the subline of her own road, the Line Lane. Pityful a rethoric question ran over her cheek, then</p>
              </div>
            </div>
          </div>
        </section>
    
      </main>
      <hr/>
      <footer>
        <h2>BOTTOM of CHILD PAGE</h2>
      </footer>
      <script src="https://ajax.aspnetcdn.com/ajax/jquery/jquery-2.2.2.min.js"></script>
      <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>
      <script>
        $(function() {
          $(".accordion").accordion({
            heightStyle: 'content',
            collapsible: true,
            active: false,
            animate: 500
          });
    
          $('.accordion').on('click', function() {
            var self = this;
            setTimeout(function() {
              xOffset = $(self).offset();
              $('body,html').animate({
                scrollTop: xOffset.top
              });
            }, 700);
          });
    
        });
      </script>
    </body>
    
    </html>