简体   繁体   English

在iframe外部选择iframe中元素的iframe父对象

[英]Select iframe parent of an element in iframe, doing outside the iframe

I have this html page: 我有这个html页面:

<div> this is the outside page </div>
<iframe id="ifr">
    (#document node)
        <html>
            <p> a node within iframe </p>
        </html>
</iframe>

Loading js in the outside page. 外部页面中加载js。 How can I select the <iframe> object, by using the <p> element inside the iframe? 如何通过使用<iframe><p>元素选择<iframe>对象?

Most web resources work on "selecting parent elements from inside iframe" (using window.parent ). 大多数网络资源都在“从iframe 内部选择父元素”上工作(使用window.parent )。 Their js is loaded within the iframe. 他们的js已加载到iframe中。 Again, I'm running the script in the outside. 同样,我在外面运行脚本。

I have managed to get the #document node - 我设法得到#document节点-

$(clicked_object).parents('html').parent()

where clicked_object means I clicked on the <p> tag inside the iframe. 其中clicked_object表示我单击了iframe中的<p>标记。 This gives the #document node of the iframe page, without any problem. 这使iframe页面的#document节点毫无问题。 But if I write 但是如果我写

$(clicked_object).parents('html').parent().parent()

it gives [] . 它给出[]

Other literatures teach how to select the <iframe> element by id (such as $('iframe#ifr') ), from the parent page. 其他文献讲授了如何从父页面上通过id选择<iframe>元素(例如$('iframe#ifr') )。 But by this way I can't get the parent of an in-iframe element. 但是通过这种方式,我无法获得iframe中元素的级。


So, one possible way is that add a onclick attribute to the <p> element, calling a parent listener function, and passing something (like #document node) as argument; 因此,一种可能的方法是在<p>元素上添加onclick属性,调用父侦听器函数,并传递某些内容(例如#document节点)作为参数; then look over all <iframe> elements matching the specific #document . 然后查看与特定#document匹配的所有<iframe>元素。

if you use javascript you could use this (assuming you used onclick or addeventlistener: 如果您使用javascript,则可以使用它(假设您使用过onclick或addeventlistener:

function something(clicked)
{
    var ifrparent = clicked.parentNode.parentNode;
}

EDIT: if the p tag is in the src of the iframe, then in your onclick function, you would use 编辑:如果p标签位于iframe的src中,则在您的onclick函数中,您将使用

var ifrparent = window.frameElement.parentNode;

and that should return the iframes parent 那应该返回iframe父对象

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

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