简体   繁体   English

GWT中的Javascript对象功能

[英]Javascript Object function in GWT

I am using DivElement on uibinder in gwt. 我在gwt的uibinder上使用DivElement。 I need to create the onclick of the div element on java class. 我需要在java类上创建div元素的onclick。 Element class provide the element.createFunction() but i could not understand how to use it ? 元素类提供element.createFunction(),但我不明白如何使用它?

**abc.ui.xml**

<g:HTMLPanel ui:field="html">
    <div class="lfloat" ui:field="label"></div>
</g:HTMLPanel>

abc.java

@UiField DivElement label;

public abc() 
{
    // TODO Auto-generated constructor stub
    initWidget(uiBinder.createAndBindUi(this));

    label.setClassName("hyperlink_labels");
    //JavaScriptObject js = label.createFunction();

    //how to create function for this DivElement

}

JavaScriptObject.createFunction() is of little to no use, as it'll return a function that does nothing. JavaScriptObject.createFunction()几乎没有用,因为它将返回一个不执行任何操作的函数。 You need to use JSNI if you need a native JS function. 如果需要本机JS函数,则需要使用JSNI。

But in your case, you should rather take advantage of event delegation: set a ClickHandler on the HTMLPanel and check whether the event target was the label or a child of it: 但是,在您的情况下,您应该利用事件委托:在ClickHandler上设置一个HTMLPanel并检查事件目标是label还是它的子级:

addClickHandler(new ClickHandler() {
  @Override
  public void onClick(ClickEvent event) {
    EventTarget target = event.getNativeEvent().getEventTarget();
    if (Node.is(target) && label.isOrHasChild(Node.as(target))) {
      // label was clicked
    }
  }
}

I think that the simplest workaround is to use Label instead. 我认为最简单的解决方法是改用Label

It uses div element so it will be rendered just like you want. 它使用div元素,因此将根据您的需要进行渲染。 As a Widget it has addClickHandler method. 作为小部件,它具有addClickHandler方法。

xml: xml:

<g:Label styleName="lfloat" ui:field="label" />

java: Java的

@UiField
Label label;

label.addClickHandler(new ClickHandler() {
    @Override
    public void onClick(ClickEvent event) {
        ...
    }
});

I would change the ui.xml 我将更改ui.xml

<g:SimplePanel ui:field="html">
    <g:SimplePanel class="lfloat" ui:field="label" />
    <!-- I am not 100% sure, if the class-attribute is named style -->
</g:SimplePanel>

and within Java 并在Java中

@UiHandler("label") public void onClick(ClickEvent e){...}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM