其他框架中的 javascript document.getElementById

发布于 2024-08-13 10:11:49 字数 281 浏览 6 评论 0原文

我有两个框架,想要从另一个框架访问一个框架中的元素:

框架 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

无妨# 2024-08-20 10:11:50

问题可能是您所在的当前框架。如果 window.frames['framename'] 不起作用,请尝试 parent.frames['framename'] 访问顶级框架。

if(parent.frames && parent.frames['framename']) {
   var elem = parent.frames['framename'].document.getElementById(...); 
   // etc
}

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
}
风透绣罗衣 2024-08-20 10:11:50

我在使用 JS 版本时遇到问题,但能够将这些示例用于有效的 jQuery 版本:

var obj = $('#yourelementid', parent.frames["framename"].document);

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);
对你而言 2024-08-20 10:11:50
document.getElementById('frameId').contentDocument.getElementById('frameChild');
document.getElementById('frameId').contentDocument.getElementById('frameChild');
请远离我 2024-08-20 10:11:50

或者,如果您想试试运气,也可以使用数字参数。

window.frames[0].document

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

window.frames[0].document

ζ澈沫 2024-08-20 10:11:50

由于某种原因

window.frames["framename"].document.getElementById ( "yourelementid" );

不适合我。

另一种方法做了:

   document.getElementById('framename').contentWindow.document.getElementById('yourelementid');

For some reason

window.frames["framename"].document.getElementById ( "yourelementid" );

was not working for me.

This other method did:

   document.getElementById('framename').contentWindow.document.getElementById('yourelementid');
夏九 2024-08-20 10:11:50

只需使用
frameId.MyElementId

当然,在定义框架时,仅使用“name=....”是不够的,而是在框架标记中使用“id=...”。例如:

    <iframe id=MyFrame> .... 
          <input id=MyElementId>
               ...

    <script>
       // do something with MyElementId
           MyFrame.MyElementId.value = "something";

至少,它在 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.:

    <iframe id=MyFrame> .... 
          <input id=MyElementId>
               ...

    <script>
       // do something with MyElementId
           MyFrame.MyElementId.value = "something";

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]

勿忘初心 2024-08-20 10:11:49

您可以使用来引用其他框架

window.frames["framename"]

,然后可以使用来引用 DOM 中的元素

window.frames["framename"].document.getElementById ( "yourelementid" );

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" );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文