如何在 iframe 中使用 jQuery 确定元素的祖先(例如,当我们检查元素以在 firebug 中查看时,它会显示完整路径)
我在 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>
我已经给出了单击事件,当单击第一个 iframe 中的任何链接时更改第二个 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);
});
通过访问 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);
}
};
现在我想要做的是我在单击事件中添加了“路径”,它显示了完成 console.log() 时元素祖先的路径,但并非在所有情况下都按预期显示。我不想像我现在所做的那样通过访问 id 来更改 src,因为这在某种程度上工作正常,但之后当我想访问选项卡时,href 变为空白,并且我没有得到预期的输出(意味着页面视图)在第二个 iframe 内不会改变)。相反,我想使用完整的 DOM 路径,如以下链接中给出的
http:// /davecardwell.co.uk/javascript/jquery/plugins/jquery-getpath/
我无法获取路径并使用它而不是访问 iframe 的 id 来更改 src。我该如何做到这一点,以便在单击第一个 iframe 中的任何选项卡或链接时可以更改第二个 iframe 的路径
I have two iframes in an html page
<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
$('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
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. 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). Instead i want to use the full DOM path like given in the following link
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. 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
听起来您很难保持 iframe 同步 - 当一个 iframe 中的 URL 更改时,您希望它更改另一个 iframe 以匹配。您可以通过仅观察 iframe 的
onload
事件来简化事情,当它触发时,更改其他框架的location
以匹配: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. You might simplify things by just watching the
onload
event of the iframe and when it fires, change thelocation
of the other frame to match: