如何使用jquery从自身中选择iframe

Rad*_*ado 3 iframe jquery

我需要在同一个iframe中选择一个带有jquery查询的iframe.但是,我不想为iframe分配唯一的ID并使用

$('#'+self.name,top.document)
Run Code Online (Sandbox Code Playgroud)

(我在某处看到过).有没有相对的方法来做到这一点(与$(this)和遍历的东西??!?)

一切都在同一台服务器上,没有xss.

RaY*_*ell 27

如果您的网站上只有一个iFrame,则很容易.只需使用此选择器

 $('iframe', parent.document)
Run Code Online (Sandbox Code Playgroud)

如果你有一个以上它会变得更复杂.iFrame文档没有关于它加载了哪个主页面iframe的线索,因此没有简单的方法用选择器选择父iframe.如果你知道父ID,姓名,类甚至iframe源网址,你可以使用该信息修改上面的选择器,只匹配你想要的iframe.如果您在主页上知道其索引,也可以匹配所需的iframe.

 // iframe with MyClass class
 $('iframe.MyClass', parent.document);

 // iframe with MyId id
 $('iframe#MyId', parent.document);
 // even better since id is unique
 $('#MyId', parent.document);

 // iframe with myname name
 $('iframe[name=myname]', parent.document);

 // iframe with specific src
 $('iframe[src=/path/to/iframe/source]', parent.document);

 // second iframe (index is starting from 0)
 $('iframe:eq(1)', parent.document); 
Run Code Online (Sandbox Code Playgroud)