javascript document.getElementById in other frames

2019-02-08 10:15发布

so, I have 2 frames and want to access to element from one frame into another :

frame 1:

<div id='someId'>...</div>

frame 2:

var div=document.getElementById('someId');

div.innerHTML='something'; 

this is somehow not functioning in Firefox so I want to be sure, can I access to element in another frame by its ID ?

4条回答
在下西门庆
2楼-- · 2019-02-08 10:23

You can refer the other frame by using

window.frames["framename"]

and then you can refer the element in the DOM by using

window.frames["framename"].document.getElementById ( "yourelementid" );
查看更多
不美不萌又怎样
3楼-- · 2019-02-08 10:25

Or, if you feel like trying your luck, you can just use a numeric parameter.

window.frames[0].document

查看更多
啃猪蹄的小仙女
4楼-- · 2019-02-08 10:29

I was having problem with the JS version but was able to use these examples for a working jQuery version:

var obj = $('#yourelementid', parent.frames["framename"].document);
查看更多
贪生不怕死
5楼-- · 2019-02-08 10:43

The issue may be the current frame you are in. If window.frames['framename'] does not work, try parent.frames['framename'] to access the top level frames.

if(parent.frames && parent.frames['framename']) {
   var elem = parent.frames['framename'].document.getElementById(...); 
   // etc
}
查看更多
登录 后发表回答