如何让IFRAME监听父iframe的click事件并使用父iframe中单击元素的路径 [英] How to make IFRAME listen to click event of parent iframe and use the path of the clicked element in parent iframe

查看:1849
本文介绍了如何让IFRAME监听父iframe的click事件并使用父iframe中单击元素的路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在同一个域中的html页面中有两个iframe

I have two iframes in my html page in the same domain

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

我已经为iframe中的一个标签提供了点击事件,我从这里得到了路径父iframe中单击的元素

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

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

我现在想要使用此路径并触发其他iframe中的click事件,以便两个iframe都具有相同的页面视图。我通过以下方式从父iframe的click事件中使用第二个iframe中的路径:

I now want to use this path and trigger the click event in other iframe so that both iframe have the same page view. I am using the path in second iframe from the click event of parent iframe in the following way:

    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');
                            }
                        }
                    }

我现在面临的问题我收到了在第二个iframe中触发的点击事件的提醒,但它没有加载父iframe的路径并打开与父iframe相同的内容。

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使用父iframe中的click事件的父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.

推荐答案

你正在做很多循环,如果jQuery会自动为你做。除非我遗漏了什么。请参阅我对每行的评论:

You are doing a lot of looping and if-ing that jQuery will do for you automatically. 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

我重新写这样:

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

细分,即:

$("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

这是你想要做的吗?如果是这样,那么我相信你的问题是你在调用 .find() .contents() $ c>。

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().

编辑请注意,您无法通过调用 $(#myLink导航到网址)。单击()。您将不得不操纵 location.href 。但是,在IE和更新版本的FireFox和Opera中,可以通过直接调用元素的本机DOM方法 .click()来执行此操作: $( A#myLink的)[0]。点击()。由于这是HTML5的一部分,您很快就可以在Chrome中执行此操作。

Note that you cannot navigate to a url by calling $("a#myLink").click(). You'll have to manipulate location.href instead. 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(). As this is part of HTML5, you'll probably be able to do so in Chrome shortly.

这是一个演示jQuery的点击行为的演示()链接上的方法: http://jsfiddle.net/gilly3/C48mv / 1 /

Here's a demo showing the behavior of jQuery's click() method on links: http://jsfiddle.net/gilly3/C48mv/1/

这篇关于如何让IFRAME监听父iframe的click事件并使用父iframe中单击元素的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆