Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使IFRAME侦听父IFRAME的click事件并使用父IFRAME中单击元素的路径_Javascript_Jquery_Iframe - Fatal编程技术网

Javascript 如何使IFRAME侦听父IFRAME的click事件并使用父IFRAME中单击元素的路径

Javascript 如何使IFRAME侦听父IFRAME的click事件并使用父IFRAME中单击元素的路径,javascript,jquery,iframe,Javascript,Jquery,Iframe,我在同一个域的html页面中有两个iFrame <iframe id="i1" width="100%" height="100%" src="/wordpress"></iframe></th> <iframe id="i2" width="100%" height="100%" src="/wordpress"></iframe></th> 现在我想使用这个路径并在其他iframe中触发click事件,以便两个if

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

<iframe id="i1" width="100%" height="100%" src="/wordpress"></iframe></th>
<iframe id="i2" width="100%" height="100%" src="/wordpress"></iframe></th>  
现在我想使用这个路径并在其他iframe中触发click事件,以便两个iframe具有相同的页面视图。我以以下方式使用父iframe的单击事件中的第二个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');
                            }
                        }
                    }
我现在面临的问题是,我收到了在第二个iframe中触发的click事件的警报,但它没有加载父iframe的路径并打开与父iframe相同的内容


我不明白我在哪里犯了错误。有没有可能按照我所尝试的方式去做呢?如果是,有人能告诉我为什么会发生这种情况,以及我需要做什么,以便另一个iframe使用父iframe中单击事件的父iframe路径打开内容

您正在进行大量的循环,如果进行循环,jQuery将自动为您提供帮助。除非我错过了什么。请参见我对每行的评论:

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()
之前,您没有在iframe上调用
.contents()

编辑:请注意,您无法通过调用
$(“a#myLink”)导航到url。请单击()。您必须操作
location.href
。但是,在IE和更新版本的FireFox和Opera中,可以通过调用元素的本机DOM方法
。直接单击()
,如下所示:
$(“a#myLink”)[0]。单击()。由于这是HTML5的一部分,您可能很快就能在Chrome中实现这一点


下面是一个演示,展示了jQuery的
click()
方法在链接上的行为:

您正在执行大量循环,如果jQuery自动执行循环,则会自动执行。除非我错过了什么。请参见我对每行的评论:

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()
之前,您没有在iframe上调用
.contents()

编辑:请注意,您无法通过调用
$(“a#myLink”)导航到url。请单击()。您必须操作
location.href
。但是,在IE和更新版本的FireFox和Opera中,可以通过调用元素的本机DOM方法
。直接单击()
,如下所示:
$(“a#myLink”)[0]。单击()。由于这是HTML5的一部分,您可能很快就能在Chrome中实现这一点


下面是一个演示,展示了jQuery的
click()
方法在链接上的行为:

感谢gilly3在我的代码中添加的注释。是的,我也觉得我做了太多的循环。在我的代码中,你问过阿哈什来自哪里?实际上,我是从click事件$('a')中获取的。click()。我已经尝试了你的代码,但没有得到结果。你说得对,我正试着按照你的建议去做,在父iframe中遵循单击元素的路径,然后在另一个iframe中触发该路径,这样两个iframe都打开了相同的内容,但不知道为什么不起作用。你在一行中评论了“为什么要立即调用click”,这是因为如果我不调用click,则不会触发click事件。我根据您的建议进行了更改,但不想使用location.href。我正在使用更新版本的firefox。通过访问父iframe元素的路径是否有其他解决方法。我很好奇为什么您反对使用
location.href
。jQuery的click方法不会导航链接-请参阅此演示:。要使用DOM方法,请尝试以下操作:
$(“iframe”,parent.document).contents().find(ahash.path)[0]。单击()。我在这段代码中看到的一个危险信号是,它将在所有iframe中触发该链接,包括触发事件的iframe。你可能会陷入无限循环。您应该只针对要操纵的iframe。感谢gilly3在我的代码中添加的注释。是的,我也觉得我做了太多的循环。在我的代码中,你问过阿哈什来自哪里?实际上,我是从click事件$('a')中获取的。click()。我已经尝试了你的代码,但没有得到结果。你说得对,我正试着按照你的建议去做,在父iframe中遵循单击元素的路径,然后在另一个iframe中触发该路径,这样两个iframe都打开了相同的内容,但不知道为什么不起作用。你在一行中评论了“为什么要立即调用click”,这是因为如果我不调用click,则不会触发click事件。我根据您的建议进行了更改,但不想使用location.href。我正在使用更新版本的firefox。通过访问父iframe元素的路径是否有其他解决方法。我很好奇为什么您反对使用
location.href
。jQuery的click方法不会导航链接-请参阅此演示:。要使用DOM方法,请尝试以下操作:
$(“iframe”,parent.document).contents().find(ahash.path)[0]。单击()。我在这段代码中看到的一个危险信号是,它将在所有iframe中触发该链接,包括触发事件的iframe。你可能会陷入无限循环。您应该只针对要操纵的iframe。