其他框架中的 javascript document.getElementById
我有两个框架,想要从另一个框架访问一个框架中的元素:
框架 1:
<div id='someId'>...</div>
框架 2:
var div=document.getElementById('someId');
div.innerHTML='something';
这在 Firefox 中无法正常工作,所以我想确定,我可以通过 ID 访问另一个框架中的元素吗?
I have two frames and want to access an element in one frame from 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 an element in another frame by its ID?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
问题可能是您所在的当前框架。如果
window.frames['framename']
不起作用,请尝试parent.frames['framename']
访问顶级框架。The issue may be the current frame you are in. If
window.frames['framename']
does not work, tryparent.frames['framename']
to access the top level frames.我在使用 JS 版本时遇到问题,但能够将这些示例用于有效的 jQuery 版本:
I was having problem with the JS version but was able to use these examples for a working jQuery version:
或者,如果您想试试运气,也可以使用数字参数。
window.frames[0].document
Or, if you feel like trying your luck, you can just use a numeric parameter.
window.frames[0].document
由于某种原因
不适合我。
另一种方法做了:
For some reason
was not working for me.
This other method did:
只需使用
frameId.MyElementId
当然,在定义框架时,仅使用“name=....”是不够的,而是在框架标记中使用“id=...”。例如:
至少,它在 2022 年(接近 2023 年)有效。
[请注意,我什至没有使用“名称”......现在这完全没有必要,除非您想使用带有多个
Simply use
frameId.MyElementId
Of course, when defining your frame, not enough to use "name=....", rather use "id=..." within your frame tag. E.g.:
At least, it works as of 2022 (almost 2023).
[Note that I didn't even use "name"... that's quite not necessary by these days, unless you want to use radio buttons with multiple <INPUT's that refer to the same mutually exclusive element]
您可以使用来引用其他框架
,然后可以使用来引用 DOM 中的元素
You can refer the other frame by using
and then you can refer the element in the DOM by using