简体   繁体   English

在GWT中按属性获取DOM元素?

[英]Get DOM Element by Attribute in GWT?

I want to add elements in GWT to the following element of a static html page: 我想将GWT中的元素添加到静态html页面的以下元素:

<div data-id="test"></div>

When I would have used this: 什么时候我会用这个:

<div id="test"></div>

then I could have accessed the element with: 然后我可以访问元素:

RootPanel.get("test").add(someElement);

This is only working when I use the id attribute. 这仅在我使用id属性时有效。

How can I access an element that uses an attribute like data-id as I showed in the first example and attach a Panel or a Widget on it? 如何在第一个示例中显示使用像data-id这样的属性的元素,并在其上附加Panel或Widget?

If you don't need to support IE6/7, then you can go with JSNI: 如果您不需要支持IE6 / 7,那么您可以使用JSNI:

Element querySelector(String selectors) /*-{
  return $doc.querySelector(selectors);
}-*/;

See http://caniuse.com/queryselector for the compat table. 有关compat表,请参见http://caniuse.com/queryselector

Maybe GWT 3.0 will add this to GWT proper as IE6/7 support will definitely be removed (it's only disabled by default in GWT 2.6) 也许GWT 3.0会将此添加到GWT,因为IE6 / 7支持肯定会被删除(它只在GWT 2.6中默认禁用)

Now that you get the Element there's unfortunately no RootPanel.get(Element) method so the only way you can get a panel widget to which you can add other widgets is to use HTMLPanel.wrap(Element) . 现在您已经获得了Element但遗憾的是没有RootPanel.get(Element)方法,因此您可以获得可以添加其他小部件的面板小部件的唯一方法是使用HTMLPanel.wrap(Element)

使用gwtquery ,你可以像这样匹配元素:

$("div[data-id='test']").add(someElement);

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

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