Run JQuery in the context of another frame

后端 未结 4 1299
孤独总比滥情好
孤独总比滥情好 2020-11-27 04:09

The client I am working with has a frameset like so...


  

        
相关标签:
4条回答
  • 2020-11-27 04:41

    Like pjb3 said, set the jQuery context. If you have nested frames, your code will look like this:

    $('*',window.parent.frames[0].frames[0].document).size();
    

    Or, better yet, make a shortcut:

    targetFrame = window.parent.frames[0].frames[0].document;
    $('*',targetFrame).size();
    
    0 讨论(0)
  • 2020-11-27 04:41

    I'm not certain about the block plugin, but can't you get hold of the relevant frame and manipulate it using the "frame tree", something like this: (from within importantFrame.htm)

    parent.theFrame.someProperty = someValue;
    

    eg:

    parent.theFrame.location.href='anotherPage.html';
    

    From what I've been reading, you might need Jquery on the target page, but you could try something like:

    parent.theFrame.block();
    

    or maybe:

    $('#theFrame').block();
    
    0 讨论(0)
  • 2020-11-27 04:53

    The jQuery function, which you more commonly call with $, takes a second argument called context, which is "which DOM element of jQuery object should I do the search in". Most of the time you omit this argument, so the context defaults to the current HTML document. When your code is executing in the iframe, the document defaults to that iframe's document. But you can easily grab the document for one of the other frames.

    For example, put this in myFrame.html, and this will remove all the h1 elements from the frame with blah.html in it. Notice the second argument to $, which is the expression that grabs the blah frame from within important frame:

    <html>
      <head>
        <script type="text/javascript" src="/javascripts/jquery.js"></script>
        <script type="text/javascript">
          function doIt() {
            $('h1', window.parent.frames[0].document).remove()
          }
        </script>
      </head>
      <body>
        <h1>My Frame</h1>
        <a href="#" onclick="doIt()">Do It</a>
      </body>
    </html>
    
    0 讨论(0)
  • 2020-11-27 04:54

    In versions of jQuery >=1.7 we cannot get events old way

    Older versions (<1.7):

    var events = $('#form').data('events');
    

    1.7 and newer:

    var events = $.fn.data($('#form'), 'events');
    
    0 讨论(0)
提交回复
热议问题