Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用GWT选择文本_Java_Javascript_Gwt - Fatal编程技术网

Java 使用GWT选择文本

Java 使用GWT选择文本,java,javascript,gwt,Java,Javascript,Gwt,这是我将其修改为JSNI的最新版本: Element content = DOM.createElement("code"); select(content); public static native void select(Element el)/*-{ var doc = $wnd.document , text = $wnd.$(el) , range, selection ; if (doc.body.createTextRa

这是我将其修改为JSNI的最新版本:

Element content = DOM.createElement("code");
select(content);

public static native void select(Element el)/*-{
    var doc =  $wnd.document
        , text = $wnd.$(el)
        , range, selection
    ;
    if (doc.body.createTextRange) {
        range = $wnd.document.body.createTextRange();
        range.moveToElementText(text);
        range.select();
    } else if ($wnd.window.getSelection) {
        selection = $wnd.window.getSelection();
        range = $wnd.document.createRange();
        range.selectNodeContents(text);
        selection.removeAllRanges();
        selection.addRange(range);
    }
}-*/;
但是,在使用时,它会抛出:

caused by: com.google.gwt.core.client.JavaScriptException: (TypeError) : Argument 1 of Range.selectNodeContents does not implement interface Node.

我的代码可能有什么问题?

传递元素的
id
,您可以在Javascript代码中使用它来获取相应的HTML元素,并将此元素传递给您的
selectNodeContents()
Javascript函数:

// Call it like this:
select(el.getId());

// And the modified select:
public static native void select(String elId)/*-{
    // ...OTHER CODE OMITTED
    range.selectNodeContents($wnd.document.getElementById(elId));
    // ...OTHER CODE OMITTED
}-*/;