以下代码用于将属性样式设置为body元素并向其注册剪切/粘贴代码,但是失败。转义时,在调试时,变量body不为null且属性“style”存在,但框架没有此子项。运行工作不能按预期工作,并且在调用setBodyContent()
时NPE被抛出public class TextAreaExt extends RichTextArea
{
private BodyElement body;
@Override
protected void onLoad()
{
super.onLoad();
body = ((FrameElement) getElement().cast())
.getContentDocument().getBody();
body.setAttribute("style", "color:gray;");
registerOnCut(body);
}
public void setBodyContent(String text)
{
body.setInnerText(text);
}
private native void registerOnCut(BodyElement element) /*-{
var that = this;
console.log("registerOnCut");
element.oncut = $entry(function(event) {
//invoke method to adjust height based on content
return false;
});
element.onpaste = $entry(function(event) {
//invoke method to adjust height based on content
return false;
});
}-*/;
}
答案 0 :(得分:0)
请勿调用onLoad
方法初始化正文,并使用addInitializeHandler
初始化正文。
this.addInitializeHandler(new InitializeHandler() {
@Override
public void onInitialize(InitializeEvent event) {
body = ((FrameElement)getElement().cast()).getContentDocument().getBody();
registerOnCut(body);
}
});