**我目前正在使用 jquery textselect 插件根据页面上任何位置的选择文本来触发警报 它工作得很好,比如:
$(document).ready(function() {
$(document).bind('textselect', function(e) {
alert(e.text);
});
});
从那时起,我必须向页面添加一个 iframe,并且我需要文本选择来处理该页面中的文本 iframe 也是如此。我正在尝试做这样的事情,但它不起作用:
$(document).ready(function() {
$('#iframeId').load(function() {
$(document.getElementById("iframeId").contentWindow).bind('textselect',function(e) {
alert(e.text);
});
});
此时我已经尝试了很多方法来引用 iframe 文档,但没有成功。有什么想法吗?**
最佳答案
您可以这样访问它:
$(document).ready(function() {
$('#iframeId').load(function() {
alert("Loaded");
$('#iframeId').contents().find("body").bind('textselect', function(e) {
alert(e.text);
});
});
});
注意:只有当 iframe 在同一域上加载某些内容时,这才有效,否则您将被同源策略阻止。
关于javascript - Jquery 并将事件绑定(bind)到 iframe,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2526726/