0

我正在使用以下代码突出显示 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>"));

            }
        });
    });
    });
4

2 回答 2

1

如果iframe1是 的 ID,则<iframe>需要将其放在选择器的引号中。

所以而不是:

$(#iframe1).live("mouseup", function () {
   //...

你需要:

$('#iframe1').live("mouseup", function () {
   //...
于 2011-01-14T18:20:48.493 回答
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>"));
        }
   })
})
于 2011-01-14T18:26:52.187 回答