Jquery并将事件绑定到iframe [英] Jquery and binding an event to an iframe

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

问题描述

**我目前正在使用jquery textselect插件根据页面
上任意位置的选择文本触发警报,并且它可以很好地执行以下操作:

**I'm currently using a jquery textselect plugin to fire alerts based on the selection text anywhere on the page and it works just fine doing something like:

$(document).ready(function() {
    $(document).bind('textselect', function(e) {
        alert(e.text); 
    });
});

我必须在页面中添加iframe,我需要选择文本进行操作
iframe中的文字也是如此。我正在尝试做类似的事情,但它不起作用:

I've since had to add an iframe to the page and I need the text selection to work on text within the iframe as well. I'm trying to do something like this but it's not working:

$(document).ready(function() {
    $('#iframeId').load(function() {
    $(document.getElementById("iframeId").contentWindow).bind('textselect',function(e) {
    alert(e.text); 
    });
});

在这一点上,我尝试了很多方法来引用iframe文档而没有任何成功。任何想法?**

At this point I've tried a whole mess of ways to reference the iframe document without any success. Any ideas?**

推荐答案

您可以这样访问:

$(document).ready(function() {
  $('#iframeId').load(function() {
    alert("Loaded");
    $('#iframeId').contents().find("body").bind('textselect', function(e) {
      alert(e.text);
    });
  });
});

注意:这仅适用于iframe在同一域上加载某些内容的情况,否则您将被同一源策略阻止。

Note: This only works if the iframe is loading something on the same domain, otherwise you're going to be blocked by the same origin policy.

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

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