javascript - Jquery and binding an event to an iframe - Stack Overflow

**I'm currently using a jquery textselect plugin to fire alerts based on the selection text anywhe

**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); 
    });
});

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); 
    });
});

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

**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); 
    });
});

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); 
    });
});

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

Share Improve this question asked Mar 26, 2010 at 21:02 dbrdbr 211 silver badge2 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

You can access it like this:

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

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.

Here's what I ended up doing just for reference... What you sent worked. The original function is passing in a document object. For an iframe document object I did something like:

window.frames["ifFrameName"].document

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745169352a4614828.html

相关推荐

  • javascript - Jquery and binding an event to an iframe - Stack Overflow

    **I'm currently using a jquery textselect plugin to fire alerts based on the selection text anywhe

    6小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信