我使用以下代码突出显示iframe中的文本,但我无法使其工作
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>"));
}
});
});
});
答案 0 :(得分:1)
如果iframe1
是<iframe>
的ID,则需要将其放在选择器的引号中。
所以而不是:
$(#iframe1).live("mouseup", function () {
//...
你需要:
$('#iframe1').live("mouseup", function () {
//...
答案 1 :(得分:0)
$( 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>"));
}
})
})