简体   繁体   English

访问jquery对话框内的iframe内的元素

[英]access an element inside of an iframe inside of a jquery dialog box

I need to "load a page" inside of a page that the user is already on instead of redirecting them to that new page. 我需要在用户已经进入的页面内部“加载页面”,而不是将他们重定向到该新页面。 It is my understanding that an iframe is the best way of doing this. 据我了解,iframe是执行此操作的最佳方法。 I like jQuery's dialog box as a wrapper for this iframe content, as it looks nice and allows the user to reposition the popup as well as close it easily. 我喜欢jQuery的对话框作为此iframe内容的包装,因为它看起来不错,并且允许用户重新放置弹出窗口并轻松关闭它。

I'm having trouble accessing the elements on this iframe however. 但是,我无法访问此iframe上的元素。 I want to be able to take data from the user's original window and add it to the new iframe's input elements, but I can't seem to find the syntax for doing this. 我希望能够从用户的原始窗口中获取数据并将其添加到新的iframe的输入元素中,但是我似乎找不到执行此操作的语法。

There is something in the code for the iframe page that overwrites jQuery's use of $ , so I can't use $('#foo').text('bar') . iframe页面的代码中有一些内容会覆盖jQuery对$的使用,因此我不能使用$('#foo').text('bar') I've been trying to find the correct syntax in pure javascript - like 我一直试图在纯JavaScript中找到正确的语法-如

var a = window.frames["e_frame"].document.getElementById ("title");
a.text = 'foobar';

where e_frame is the id I've given to my iframe, and title is the id of an element that loads in the iframe. 其中e_frame是我赋予iframe的ID, title是加载到iframe中的元素的ID。 I can see these both clearly in the developer console, but I cannot access them and I'm not sure what I'm doing wrong. 我可以在开发人员控制台中清楚地看到它们,但是我无法访问它们,并且不确定自己做错了什么。 Typing in the command above in the console gives me this error: 在控制台中输入上面的命令会给我这个错误:

VM364:1 Uncaught TypeError: Cannot read property 'document' of undefined
    at <anonymous>:1:28

and typing window.frames into the console to debug myself gives me way too many lines to go through since I'm not sure what I'm looking for. 在控制台中键入window.frames进行自我调试会给我带来太多的麻烦,因为我不确定要查找的内容。

Any ideas? 有任何想法吗?

Have you tried this ? 你试过这个吗?

var iframe = document.getElementById("e_frame");
var iframe_contents = iframe.contentDocument.getElementById('title');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM