简体   繁体   English

如何在iframe中使用jQuery确定元素的祖先(例如,当我们检查元素以在Firebug中查看时,它会显示完整路径)

[英]how to determine an element’s ancestors with jQuery in an iframe (eg. like when we do inspect element to view in firebug it shows the full path)

I have two iframes in an html page 我在html页面中有两个iframe

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

I have given the click event to change the href of second iframe when any link within first iframe is clicked so that both iframe have the same page loaded 当单击第一个iframe中的任何链接时,我给出了click事件来更改第二个iframe的href,以便两个iframe都加载了相同的页面

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

The href is changed by the following method by accessing the id 通过访问ID通过以下方法更改href

document.sendevent=function(e, ahash){
   if (ahash.id!=undefined) {
      $('#i2', window.parent.document).contents().find('#'+ahash.id).trigger(e);
   } else {
      $('#i2', window.parent.document).attr('src', ahash.href);
   }
};

Now what i want to do is i have added "path" in the click event which shows the path of an elements ancestor when done console.log() but not in all cases as desired. 现在我想做的是,我在click事件中添加了“路径”,该事件显示了console.log()完成后元素祖先的路径,但并非在所有情况下都符合要求。 I dont want to change the src by accessing the id as i am doing now because this is working fine to a certain point but after that when i want to access tab the href gets blank and i dont get the expected output (means the page view within second iframe does not change). 我不想像现在一样通过访问id来更改src,因为这在一定程度上可以正常工作,但是在此之后,当我要访问选项卡时href变为空白并且我没有得到预期的输出(意味着页面视图在第二个iframe中不会更改)。 Instead i want to use the full DOM path like given in the following link 相反,我想使用以下链接中给出的完整DOM路径

http://davecardwell.co.uk/javascript/jquery/plugins/jquery-getpath/ http://davecardwell.co.uk/javascript/jquery/plugins/jquery-getpath/

I am unable to fetch the path and use it instead of accessing the iframe's id to change the src. 我无法获取路径并使用它,而不是访问iframe的ID来更改src。 How can i do that so that i can change the path of the second iframe when clicked any tab or link within first iframe 我该怎么做,以便在单击第一个iframe中的任何标签或链接时可以更改第二个iframe的路径

It sounds like you are having difficulty keeping your iframes in sync - when the URL changes in one, you want it to change the other to match. 听起来您很难将iframe保持同步-当其中一个网址更改时,您希望它更改另一个以匹配。 You might simplify things by just watching the onload event of the iframe and when it fires, change the location of the other frame to match: 您可以通过仅观察iframe的onload事件来简化操作,并在触发iframe时更改另一个框架的location以使其匹配:

$("#i1").load(function (e) {
    var url = this.contentWindow.location.href;
    var frameLoc = $("#i2")[0].contentWindow.location;
    if (frameLoc.href != url) {
        frameLoc.href = url;
    }
});

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

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