jquery iframe src [英] jquery iframe src

查看:315
本文介绍了jquery iframe src的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含iframe的HTML页面,我正在尝试在iframe中的链接上编写函数。这些函数将在HTML页面中进行更改...

I have an HTML page containing an iframe and I am trying to write functions on links in the iframe. The functions will make changes in the HTML page...

以下脚本适用于在iframe中单击的第一个链接。但是当iframe变为src时,该函数将不再起作用。

The following script works on the first link that is clicked in the iframe. But when the iframe chages "src" the function will not work any more.

即使iframe的src发生了变化,我怎样才能使它工作?

How can I make it work even if the src of the iframe is changed?

(所有页面都在同一个域中)

(all pages are on the same domain)

<script type="text/javascript">
  $("iframe").contents().find("a").click(function() {
    alert('Load was performed.');
  });
</script>


推荐答案

尝试使用.live()方法,它将事件绑定到现有元素和未来元素。当您更改src时,您需要再次将click事件绑定到新链接。

try to use the .live() method, it binds an event to the existing elements and future elements. When you change the src, you need to bind again the click event to the new links.

我不知道.live()方法是否适用于iframe但你可以试试这个:

I don't know if the .live() method works on a iframe but you can try this:

  $("iframe").contents().find("a").live('click',function() {
    alert('Load was performed.');
  });

抱歉我的英语不好!

我不知道它是否会起作用(抱歉我现在没有测试的地方),但如果你试图在iframe的onLoad事件上绑定一个函数会发生什么,(即使W3C说iframe有没有事件,它似乎得到所有浏览器的支持):

i don't know if it will works (sorry I don't have where to test things now), but what happens if you try to bind an function on the onLoad event of your iframe, (even if W3C says that iframe has no events, it appears to be supported by all browsers):

    <iframe src="/my_contents" onLoad="onload_function()"></iframe>
    ...
   <script>
        onload_function(){
          $(function(){
            $("iframe").contents().find("a").click(function() {
              alert('Load was performed.');
            });
          });
        }
    </script>

这篇关于jquery iframe src的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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