javascript 使用对框架的 jQuery 引用访问框架中的元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3358680/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 00:57:10  来源:igfitidea点击:

Accessing an element in a frame using a jQuery reference to the frame

javascriptjquery

提问by Tony_Henrich

I have a frame that's nested in an iFrame. <iframe>..<frameset..><frame id="report">. I have a jQuery reference, myFrame, to the report frame. I don't have control to the html. I am trying to modify an element in the frame. contents() doesn't work with frames. I tried using a context like myFrame.document & myFrame.contentDocument but they give errors.

我有一个嵌套在 iFrame 中的框架。 <iframe>..<frameset..><frame id="report">. 我有一个 jQuery 引用 myFrame 到报告框架。我无法控制 html。我正在尝试修改框架中的元素。contents() 不适用于框架。我尝试使用像 myFrame.document & myFrame.contentDocument 这样的上下文,但它们给出了错误。

How do I reference the frame's document or content using jQuery? Is a jQuery reference different than a javascript reference?

如何使用 jQuery 引用框架的文档或内容?jQuery 引用与 javascript 引用不同吗?

ADDITION:

添加:

EVERYTHING IS IN THE SAME DOMAIN. I know about the browser's cross domain security block.

一切都在同一个域中。我知道浏览器的跨域安全块。

ADDITION2:

附加2:

I had some html code in my original question and didn't notice they got hidden.

我的原始问题中有一些 html 代码,但没有注意到它们被隐藏了。

采纳答案by Tony_Henrich

This is the selector:

这是选择器:

$(top.window.frames['frameID'][1].document).contents()[0].find('elementinFrame')

Although it doesn't work every single time and I am still find out why.

虽然它不是每次都有效,但我仍然找出原因。

回答by meder omuraliev

Is your frame that's inside your frame on the same domain as the container of the frame? You should know by now if they aren't on the same domain you can't manipulate it due to security limitations by the browser.

框架内的框架是否与框架的容器位于同一域中?您现在应该知道如果它们不在同一个域中,由于浏览器的安全限制,您无法对其进行操作。

Is a jQuery reference different than a javascript reference?

jQuery 引用与 javascript 引用不同吗?

jQuery IS Javascript, it does a lot of branching under the core so you don't have to do a lot of the dirty work.

jQuery 是 Javascript,它在核心下做了很多分支,所以你不必做很多肮脏的工作。

Edit: Since no code was provided... had to make a demo myself.

编辑:由于没有提供代码......不得不自己制作一个演示。

http://medero.org/frame.html

http://medero.org/frame.html

$('iframe').contents().find('frame').get(0).contentWindow.document

Didn't test in IE.

没有在IE中测试。

For clarification...

为了澄清...

  • $('#el')returns a jQuery object.
  • $('#el').get(0)returns the first element in the jQuery array-like object, a reference to the real DOM element.
  • $('#el')返回一个 jQuery 对象。
  • $('#el').get(0)返回 jQuery 类数组对象中的第一个元素,即对真实 DOM 元素的引用。

回答by Keyo

Yo dawg...The browser security model will prevent any DOM access to an iFrame from a different domain. This is to prevent somebody embedding a page and them sabotaging the content/layout, capturing form data and so on.

Yo dawg...浏览器安全模型将阻止对来自不同域的 iFrame 的任何 DOM 访问。这是为了防止有人嵌入页面并破坏内容/布局、捕获表单数据等。

If you have access to the source of the iFramed page you can (via a third iFrame on the same domain as the browser viewport) send information to the top page via url parameters. Otherwise you're out of luck.

如果您有权访问 iFramed 页面的源,则可以(通过与浏览器视口位于同一域中的第三个 iFrame)通过 url 参数将信息发送到首页。否则你就倒霉了。

There isn't much point doing this if you just want to change the layout of the page since you'll have css access anyway. But it is useful for setting the height of the iFrame to whatever the scroll height of the iFrame is.

如果您只想更改页面的布局,那么这样做没有多大意义,因为无论如何您都将拥有 css 访问权限。但是将 iFrame 的高度设置为 iFrame 的滚动高度时很有用。

EditI think this is what you were looking for: http://simple.procoding.net/2008/03/21/how-to-access-iframe-in-jquery/

编辑我认为这就是你要找的:http: //simple.procoding.net/2008/03/21/how-to-access-iframe-in-jquery/

$('#iframeID').contents().find('#someID').html();