简体   繁体   English

如何使IFRAME侦听父iframe的click事件并使用父iframe中clicked元素的路径

[英]How to make IFRAME listen to click event of parent iframe and use the path of the clicked element in parent iframe

I have two iframes in my html page in the same domain 我在同一个域的html页面中有两个iframe

<iframe id="i1" width="100%" height="100%" src="/wordpress"></iframe></th>
<iframe id="i2" width="100%" height="100%" src="/wordpress"></iframe></th>  

I have given the click event for the a tag inside the iframe from where i am getting the path of the clicked element in the parent iframe 我已经在iframe中为a标签赋予了click事件,从那里我可以获取父iframe中clicked元素的路径

$('a').click(function(e){
        var path = $(this).getPath();
        var ahash={
            'path':path 
        };
        if (getFrameElement())
            window.parent.document.Aaddevent(e, ahash);
});

I now want to use this path and trigger the click event in other iframe so that both iframe have the same page view. 现在,我想使用此路径并在其他iframe中触发click事件,以便两个iframe具有相同的页面视图。 I am using the path in second iframe from the click event of parent iframe in the following way: 我正在通过以下方式使用父iframe的click事件在第二个iframe中使用路径:

    var iframes= parent.document.getElementsByTagName('iframe');
                    for (var i= iframes.length; i--;) {
                        var iframe= iframes[i];
                        if($(iframe)){
                            if (ahash.path!=undefined) {
                               $(iframe).click(function(e) {
                                     $(this).find(ahash.path).trigger('click');
                               }).click();
                               $(iframe).unbind('click');
                            }
                        }
                    }

The problem i am facing now is i am getting the alert for the click event getting triggered in the second iframe but it is not loading the path of the parent iframe and opening the same content as parent iframe. 我现在面临的问题是,我收到第二个iframe中触发的click事件的警报,但它没有加载父iframe的路径并打开与父iframe相同的内容。

I am not understanding where i am doing the mistake. 我不明白我在哪里做错。 Is it possible to do the way i am tring to make it happen? 有可能按照我想要的方式来实现它吗? If so can anyone suggest me why this is happening and what i need to do so that the other iframe opens the content using the path of the parent iframe from the click event in the parent iframe. 如果是这样,谁能建议我为什么会这样,以及我需要做什么,以便其他iframe使用父iframe中来自click i事件中父事件的路径打开内容。

You are doing a lot of looping and if-ing that jQuery will do for you automatically. 您正在做很多循环,而jQuery将自动为您做if-ing。 Unless I am missing something. 除非我缺少任何东西。 See my comments on each line: 看到我在每一行的评论:

var iframes= parent.document.getElementsByTagName('iframe');  // why not use jQuery?
for (var i= iframes.length; i--;) { // why not use jQuery here?
    var iframe= iframes[i]; 
    if($(iframe)) { // always true - even an empty jQuery object returns true
        // where does ahash come from? 
        if (ahash.path!=undefined) { // Why not do this check outside the loop?
            $(iframe).click(function(e) { 
                $(this).find(ahash.path).trigger('click'); // this will do nothing
                    // $(this).find() will be empty - your iframe has no children
                    // you want $(this).contents().find();
            }).click();                // why immediately call click and then unbind?
            $(iframe).unbind('click'); // why not just call the code?
        } // is this brace in the right spot?
    // missing brace
// missing brace

I'd re-write it like this: 我会这样重写它:

$("iframe", parent.document).contents().find(ahash.path).click();

Broken down, that is: 细分为:

$("iframe", parent.document) // get all of the iframes in the parent document
    .contents()              // get the content documents of those frames
    .find(ahash.path)        // find the elements matching the ahash.path in each
                             // of the frames' content documents
    .click();                // trigger a click on the matching elements in each frame

Is that what you are trying to do? 那是你想做的吗? If so, then I believe your problem is that you are not calling .contents() on your iframe before calling .find() . 如果是这样,那么我认为您的问题是您在调用.find()之前没有在iframe上调用.contents() .find()

Edit: Note that you cannot navigate to a url by calling $("a#myLink").click() . 编辑:请注意,您无法通过调用$("a#myLink").click()导航到URL。 You'll have to manipulate location.href instead. 您必须改为操作location.href However, in IE and newer versions of FireFox and Opera you can do so by calling the element's native DOM method .click() directly like this: $("a#myLink")[0].click() . 但是,在IE和FireFox和Opera的较新版本中,您可以通过直接调用元素的本机DOM方法.click()$("a#myLink")[0].click() As this is part of HTML5, you'll probably be able to do so in Chrome shortly. 由于这是HTML5的一部分,因此您很快就可以在Chrome中做到这一点。

Here's a demo showing the behavior of jQuery's click() method on links: http://jsfiddle.net/gilly3/C48mv/1/ 这是一个演示jQuery的click()方法在链接上的行为的演示: http : //jsfiddle.net/gilly3/C48mv/1/

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

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