简体   繁体   English

Gquery-实时方法,如何正确使用它

[英]Gquery - live method, how to use it properly

I'm experiencing with Gquery and find something difficult to work with live() method. 我在使用Gquery时遇到问题,发现使用live()方法很难工作。 As far as I'm concerned, it is plenty useful when you want to define some behavior for "future" elements. 就我而言,当您要为“未来”元素定义某些行为时,它很有用。 for example, I would like to change background color to any horizontal panel I create after clicking certain button. 例如,我想将背景色更改为单击某些按钮后创建的任何水平面板。 Sample code is attached below. 示例代码附在下面。

RootPanel.get(); //before invoking live method, recommended in gwt forum
GQuery hPanel = mySelectors.getHorizontalPanel();
hPanel.live(Event.ONCLICK, new Function () {
    public boolean f (Event e) {
    int red = 100;
    int green = 100;
    int blue = 100;
    while (red < 250) {
        while (green < 250) {
            while (blue < 250) {
                String bgColor = "backgroundColor: 'rgb(" + red + "," + green + "," + blue + ")'";
                $(e).animate(bgColor,8);
                blue+=10;
            }
            green+=10;
        }
        red+=10;
    }               
    return false;
    }
});

HorizontalPanel horizontalPanel = new HorizontalPanel();
horizontalPanel.setHeight("40px");
horizontalPanel.setWidth("40px");
horizontalPanel.setStyleName("horizontal-panel");

final HorizontalPanel newPanel = new HorizontalPanel();
newPanel.add(horizontalPanel);
RootPanel.get("horizontalPanels").add(newPanel);

For some reason, no event is invoked when I click over the panel. 由于某些原因,当我在面板上单击时,不会调用任何事件。 The selector was declared in the following way: 选择器以以下方式声明:

@Selector(".horizontal-panel")
GQuery getHorizontalPanel();

Any help, or light over this issue would be far appreciated, 对于此问题的任何帮助或启发,将不胜感激,

Thanks in advance! 提前致谢!

I think it's because live doesn't support selectors... 我认为这是因为live不支持选择器...

Could you try to replace this part of code : 您可以尝试替换这部分代码:

GQuery hPanel = mySelectors.getHorizontalPanel();
hPanel.live(Event.ONCLICK, new Function () {

by : 创建人:

$(".horizontal-panel").live(Event.ONCLICK, new Function () {

live() needs to know the css selectors. live()需要知道CSS选择器。 If you use @Selector, it loose this information as GQuery replace it at compile time by the most optimized way to query the elements 如果您使用@Selector,则会因为GQuery在编译时以最优化的查询元素的方式替换它而失去了该信息。

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

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