2010-04-18 5 views
3

크로스 플랫폼 및 버전 독립적 인 클릭 이벤트를 문서에 등록해야합니다. 나는 두 개의 텍스트 상자를 가지고 버튼을 제출하지만 난 두 개의 텍스트 상자의 바깥 쪽을 클릭하고 버튼을 제출할 때 다음 경고) (괜찮나 내가 GWT하여이를 achive 수gwt click 이벤트 문서

document.get을 표시 의미 . addMouseClick ???

답변

1

마음에 오는 가장 쉬운 방법은 FocusPanel 모든 것을 래핑하는 것입니다 :

ClickHandler clickHandler = new ClickHandler() { 
     @Override 
     public void onClick(ClickEvent event) { 
      Window.alert("TextBox/Button clickHandler."); 
      event.stopPropagation(); // The important line - We stop the event 
            // propagation here so that the FocusPanel 
            // doesn't get the event 
     } 
    }; 
TextBox textBox = new TextBox(); 
textBox.addClickHandler(clickHandler); 
Button button = new Button("Test"); 
button.addClickHandler(clickHandler); 


// Since FocusPanel is a SimplePanel, it can only have one child, so we are 
// wrapping everything additionally in a HorizontalPanel 
HorizontalPanel hPanel = new HorizontalPanel(); 
hPanel.add(textBox); 
hPanel.add(button); 


FocusPanel focusPanel = new FocusPanel(hPanel); 
focusPanel.addClickHandler(new ClickHandler() { 
    @Override 
    public void onClick(ClickEvent event) { 
     Window.alert("Outside."); // Clicked outside of the TextBox/Button 
    } 
}); 

RootPanel.get().add(focusPanel);  

단점은 당신이 (당신이 경고를 원하지 않는 모든 요소에 ClickHandler의를 수행 할 수 있습니다 할당해야한다는 것입니다 위에서처럼 메모리를 절약하기 위해 동일한 ClickHandler을 사용하십시오. 그 외에도 FocusPanel 구현은 onclick 비헤이비어가 교차 브라우저로 유지되도록해야합니다.