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 に答える