开发者

GWT clickable link (anchor) without href

I'm trying to create a link (anchor) in GWT that can be clicked and it's ClickEvent can be processed, while at the same time this anchor wouldn't reload the page. This basically means that a href must not be set.

In javascript this is done like this:

<a href="javascript:handleAnchorClick();">Link</a>开发者_高级运维; 

or with

<a href="#" onclick="return handleAnchorClick()">Link</a>  

where handleAnchorClick() returns false.

What would be the best way to achieve this in GWT?


Use tha Anchor element, and call its addClickListener() method and add in what ever logic you wish. This sort of anchor doesn't reload the page.


Listeners are deprecated in GWT use handlers

Something like that :

Anchor a = new Anchor("hi");
a.addClickhandler(new ClickHandler() {
     @Override
     public void onClick(ClickEvent event) {
           Window.alert("hi");
     }

});


As you describe, using href="#" will reload the page.

This is what you can do in UIBinder:

    <g:Anchor ui:field="myScriptedAnchor" href="javascript:;">
        MyScriptedAnchorText
    </g:Anchor>

Then you can do whatever you want handling the ClickEvent in the view implementation.

    @UiHandler("myScriptedAnchor")
    void onMyScriptedAnchorClick(ClickEvent event) {
        // TODO whatever you want to do...
    }


Don't forget to add a style to anchor otherwise it doesnt behave like a normal html link:

.anchor {
            text-decoration: underline;
            font-weight: bold;
            cursor: pointer;
            display: block;
        }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜