拦截框架中的链接点击

发布于 2024-09-26 17:52:46 字数 814 浏览 1 评论 0原文

我有一个旧式页面,它使用包含顶部框架和底部框架的框架集。框架集在“index.html”中定义,我的代码如下:

<html>
<head>

<script src="jquery-1.4.2.min.js" type="text/javascript"></script>

<script>

    $(document).ready(function(){

        $('#mainFrame').ready(function() {

            $('#mainFrame a').live('click', function() {
                alert('u click');
                return false;
            }); 

        });

    });

</script>

</head>
<frameset id="main" rows="125,*" cols="*">
<frame src="header.html" name="headerFrame" id="headerFrame" />
<frame src="main.html" name="mainFrame" id="mainFrame"  />
</frameset>
</html>

我希望能够拦截在框架“mainFrame”上单击的链接。我以为我可以为其添加一个就绪事件,然后实时绑定单击事件,但它不起作用。有什么想法吗?

注意:这些文件都位于同一域中,因此这不是 XSS 问题。

I have a old fashion page that uses a frameset containing a top frame and bottom frame. The frameset is defined in "index.html" and my code is as follows:

<html>
<head>

<script src="jquery-1.4.2.min.js" type="text/javascript"></script>

<script>

    $(document).ready(function(){

        $('#mainFrame').ready(function() {

            $('#mainFrame a').live('click', function() {
                alert('u click');
                return false;
            }); 

        });

    });

</script>

</head>
<frameset id="main" rows="125,*" cols="*">
<frame src="header.html" name="headerFrame" id="headerFrame" />
<frame src="main.html" name="mainFrame" id="mainFrame"  />
</frameset>
</html>

I would like to be able to intercept links that are clicked on the frame "mainFrame". I thought I could just add a ready event for it then live bind click events but it doesn't work. Any ideas?

Note: The files are all on the same domain so this is not an XSS issue.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

巷雨优美回忆 2024-10-03 17:52:46
<script>
$(document).ready(function(){
  $('#mainFrame').ready(function() {
    var f = document.getElementById('mainFrame').contentWindow.document;
    $('a', f).live('click', function() {
      alert('u click');
      return false;
    });
  });
});
</script>

您只需要向 jQuery 语句添加一个上下文,告诉它在哪里查找 #mainFrame a

哦,是的......您肯定需要确保 iframe 已加载。 “live”应该解决这个问题......但我现在正在做一些事情,我必须为 jQuery 编写一个自定义的 ready() 插件,以确保我的框架已加载,因为我在使用 Chrome 时遇到了问题。

Chrome 和 IE 似乎用 document.addEventListener("DOMContentLoaded"... 来抢先一步...我相信这就是 jQuery 中的 read() 函数所使用的。

无论如何,只要确保你的 iframe 完全加载即可。

<script>
$(document).ready(function(){
  $('#mainFrame').ready(function() {
    var f = document.getElementById('mainFrame').contentWindow.document;
    $('a', f).live('click', function() {
      alert('u click');
      return false;
    });
  });
});
</script>

You just need to add a context to jQuery statement, to tell it where to look for #mainFrame a

Oh yeah....you definitely need to make sure that the iframe is loaded. The "live" should take care of that...but I'm working on something now where I had to write a custom ready() plugin for jQuery to make sure my frames were loaded, because I was having trouble with Chrome.

Chrome and IE seem to jump the gun on with the document.addEventListener("DOMContentLoaded"... which I believe is what the ready() functions in jQuery use.

Anyways, just make sure that your iframes get loaded all the way.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文