Javascript jQuery未检测到Iframe
我使用下面的代码来突出显示Iframe中的文本,但我无法让它工作Javascript jQuery未检测到Iframe,javascript,jquery,css,iframe,textrange,Javascript,Jquery,Css,Iframe,Textrange,我使用下面的代码来突出显示Iframe中的文本,但我无法让它工作 function getSelectedText() { if (window.getSelection) {; return window.getSelection().toString(); } else if (document.getSelection) {; return document.getSelection();
function getSelectedText() {
if (window.getSelection) {;
return window.getSelection().toString();
} else if (document.getSelection) {;
return document.getSelection();
} else if (document.selection) {;
return document.selection.createRange().text;
}
}
$(document).ready(function () {
$("#iframe1").live("mouseup", function () {
selection = getSelectedText();
if (selection.length >= 3) {
$(this).html($(this).html().replace(selection, "<span class='highlight'>" + selection + "</span>"));
}
});
});
});
函数getSelectedText(){
if(window.getSelection){;
返回窗口.getSelection().toString();
}else if(document.getSelection){;
return document.getSelection();
}如果(文件选择){;
return document.selection.createRange().text;
}
}
$(文档).ready(函数(){
$(“#iframe1”).live(“mouseup”,函数(){
selection=getSelectedText();
如果(selection.length>=3){
$(this.html($(this.html().replace(selection,“+selection+”));
}
});
});
});
如果
iframe1
是
的ID,则需要将其置于选择器的引号中
因此,不是:
$(#iframe1).live("mouseup", function () {
//...
你需要:
$('#iframe1').live("mouseup", function () {
//...
$(函数(){
var myiframe=$($.browser.msie?frames[“iframe1”]:“#iframe1”);
//在歌剧中不起作用
加载(函数(){
selection=getSelectedText();
//下面的代码有效,但我不知道$(这个)是否有效,但你可以试试你的代码
//var w=this.contentWindow;
//如果(!w)w=myiframe[0];//IE
//警报(“锚数量:+w.$(“a”).size());
//警报(“文件标题:+w.Document.title”);
如果(selection.length>=3){
$(this.html($(this.html().replace(selection,“+selection+”));
}
})
})
Opps。。。!忘了在这里更新,但实际上我试过了,但它不起作用。它不起作用,或者我想我不知道如何实现它。我只是复制了代码的w洞并将其粘贴到$(#iframe1).live(“mouseup”,function(){这是正确的方法吗?
$( function() {
var myiframe = $($.browser.msie ? frames["iframe1"] : "#iframe1");
// doesn't work in Opera < 9
myiframe.load( function() {
selection = getSelectedText();
//below code works but i dont know about $(this) will work or not but you can try your code
//var w = this.contentWindow;
//if(!w) w = myiframe[0]; // IE
//alert("Number of anchors: " + w.$("a").size());
//alert("Document title: " + w.document.title);
if (selection.length >= 3) {
$(this).html($(this).html().replace(selection, "<span class='highlight'>" + selection + "</span>"));
}
})
})