简体   繁体   English

如何使用GWT获取“父”元素

[英]How to get the “parent” element with GWT

Say I have html part in my GWT application: 说我的GWT应用程序中有html部分:

    <ul>
        <li id="tray-active"><a data-field="home">Homepage</a></li> <!-- Active page -->
        <li><a data-field="news">News</a></li>
        <li><a data-field="products">Products</a></li>
        <li><a data-field="sales">General Sales T&C</a></li>
        <li><a data-field="job">Job Opportunities</a></li>
    </ul>

And I am able to get the AnchorElement, say for the HomePage: 我能够获得AnchorElement,比如HomePage:

@Inject @DataField Anchor home;

How can I get the List li element? 如何获得List li元素?

Is it: home.getParent(); // which returns a widget 它是: home.getParent(); // which returns a widget home.getParent(); // which returns a widget ? home.getParent(); // which returns a widget Then cast it to a UListElement ? 然后将其转换为UListElement Is that it? 是吗?

Update: 更新:

My main goal is to set the id of the li element to "tray-active", so when anchor item is clicked the "parent" li element is set to to "tray-active" 我的主要目标是将li元素的id设置为“tray-active”,因此当单击锚点项时,“parent” li元素被设置为“tray-active”

Need not to cast any thing. 无需投放任何东西。

you can do this: 你可以这样做:

home.getParent().getElement().setId("tray-active");

API API

This should give you the Element object you want : 这应该为您提供所需的Element对象:

anchor.getElement().getParentElement()

where anchor is your anchor element. 锚是你的锚元素。

anchor.getElement() 

Gives you the Anchor's element and 给你Anchor的元素和

anchor.getElement().getParentElement() ;

will give you the Element parent to the anchor which in your case should be the li Element 将为您提供锚的Element父级,在您的情况下应该是li元素

Now if you want to set an Attribute to this element such as id do this: 现在,如果要为此元素设置属性,例如id,请执行以下操作:

anchor.getElement().getParentElement().setAttribute("id", "li_id") ;

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

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