它的可能性。在事件觸發時添加到頁面主體ondragover和ondragleave偵聽器併爲多個組件添加樣式。 SSCCE:
@com.vaadin.annotations.JavaScript("summer.js")
public class Valo4UI extends UI
{
@Override
protected void init(VaadinRequest request)
{
VerticalLayout layout = new VerticalLayout();
Panel panel = new Panel("Login");
panel.setSizeUndefined();
panel.setContent(layout);
TextField username = new TextField();
username.setStyleName("canReceiveFile");
layout.addComponent(username);
TextField password = new TextField();
password.setStyleName("canReceiveFile");
layout.addComponent(password);
Button ok = new Button("Login");
ok.setStyleName("canReceiveFile");
layout.addComponent(ok);
setContent(panel);
JavaScript.getCurrent().execute("initDragStyling()");
}
...
}
summer.js:
function ohItsSummer(){
var summerTable = document.getElementsByClassName("canReceiveFile");
for(var i=0; i<summerTable.length; i++){
summerTable[i].style.borderColor = 'orange';
}
}
function itsReallyHot(){
var summerTable = document.getElementsByClassName("canReceiveFile");
for(var i=0; i<summerTable.length; i++){
summerTable[i].style.borderColor = 'inherit';
}
}
function initDragStyling(){
document.body.setAttribute("ondragover","ohItsSummer()");
document.body.setAttribute("ondragleave","itsReallyHot()");
}
要是你看看easyupload插件? https://vaadin.com/directory#!addon/easyuploads –