问题
In GWT apparently you have to use some Flash-component to alter the clipboard. Since I don't want to use Flash, but I do want to copy and paste text from my application, I would like to set text selected if someone clicks on it. The only thing the user has to do is to type Ctrl-C/Ctrl-V to copy and paste (in Windows). Is this possible? How do I do this?
More info:
The app in which I would like to use this is at http://borkent-app2.appspot.com/. It is an app I made to teach myself Hebrew words (my native language is Dutch). The Hebrew word is placed on the RootPanel using a Label. Sometimes I would like to copy and paste the Hebrew word (when I want to search on it in Google for example), but selecting it is somewhat cumbersome (probably because of the right-to-left text direction). So I would like to select the text of the Hebrew word by only clicking on the Label it is in.
回答1:
Something like this?
public void onModuleLoad() {
final Label word = new Label("some text");
word.addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
markText(word.getElement());
}
});
word.getElement().setId("test");
RootPanel.get().add(word);
}
private native void markText(Element elem) /*-{
if ($doc.selection && $doc.selection.createRange) {
var range = $doc.selection.createRange();
range.moveToElementText(elem);
range.select();
} else if ($doc.createRange && $wnd.getSelection) {
var range = $doc.createRange();
range.selectNode(elem);
var selection = $wnd.getSelection();
selection.removeAllRanges();
selection.addRange(range);
}
}-*/;
来源:https://stackoverflow.com/questions/3987824/set-selected-text-in-gwt-in-order-to-make-copy-paste-easier