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.