How to access frame (not iframe) contents from jQuery

痴心易碎 提交于 2019-11-28 08:08:16

You could grab the Frame and div you are wanting to manipulate and pass it into a variable.

var statusText = top.frames["treeStatus"].document.getElementById('statusText');

Then you can do anything you want to it through jQuery.

$(statusText).whatever();

Though sometimes you just can't get around having to use frames, keep in mind that the <frame> tag is obsoleted in HTML5. If you ever plan on moving up to HTML5, you'll have to use iFrames.

You need to supply a reference to the frame you what to access:

$("some selector", top.frames["treeStatus"]))

I have nested frames. In my case, to make it work i used command:

var statusText = 
top.document.getElementById("treeStatus").contentDocument.getElementById("statusText");

Then, as Charles already answered, you can do anything you want to it through jQuery:

$(statusText).whatever();

https://jamesmccaffrey.wordpress.com/2009/07/30/cross-frame-access-with-jquery/

    $(document).ready(function(){
     $("#Button1").click(function(){
      $(parent.rightFrame.document).contents().find(‘#TextBox1’).val(‘Hello from left frame!’);
     });
    });

But I used :

    $.post("content_right.php",{id:id},function(data)
     $(parent.frames["content_right"].document.body).html(data) ;
    });

For a pure jquery solution (that doesn't require top.frames etc), the following seems to work:

$('some selector for item from frame' ,$('frame selector')[0].contentDocument)

This has the advantage that it works for nested frames:

$('inner frame item selector', $('inner frame selector', $('outer frame selector')[0].contentDocument)[0].contentDocument)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!