在GWT中通过CSS选择器查找元素 [英] Find an element by CSS selector in GWT

查看:208
本文介绍了在GWT中通过CSS选择器查找元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用CSS选择器(例如#someId .className a)在GWT中获取任意元素。

I'm trying to grab an arbitrary element using a CSS selector (eg. "#someId .className a") in GWT.

JS小部件,可以居住在第三方网站上,并希望能够与页面上的元素进行交互。搜索JavaDocs我没有看到任何可以找到一个元素的选择器。我确实遇到过GQuery,但似乎项目可能死了

I'm building a JS widget that can live on a 3rd party website and want to be able to interact with elements on the page. Searching through the JavaDocs I don't see anything that can find an element by selector. I did come across GQuery, but it seems like the project might be dead and I'm not sure if it works with GWT 2.

我考虑的一个选择是包装一个现有的库(jQuery,Mootools,Prototype,等)转换为GWT类,并通过JSNI暴露所需的行为。

One option I've considered is wrapping an existing library (jQuery, Mootools, Prototype, etc) into a GWT class and exposing the desired behavior through JSNI. It seems this might be very builky.

任何人都有使用GWT中的通用CSS选择器的经验。

Anyone have experience using generic CSS selectors in GWT?

推荐答案

DOM 类,它提供了许多用于访问DOM树的包装器方法。没有任何函数采用一个CSS选择器jQuery风格,我知道--GWT只是鼓励/强制通过Widgets(等)访问DOM元素,而不是直接 - 虽然我知道,在你的情况下,这种低级的方法可能需要。我唯一的方法是通过纯Java GWT方法,通过许多(可怕的)链接/调用的 DOM 类。如果你只需访问一些 id ,那么会更容易 - 因为有 RootPanel.get(id) (和 DOM.getElementById(id) ,它们在他们返回的对象类型上有所不同)。

There's the DOM class, that provides many wrapper methods for accessing the DOM tree. There's no function that takes a CSS selector jQuery style that I'm aware of - GWT just encourages/enforces accessing DOM elements through Widgets (and such), not directly - though I understand that in your case such "low-level" approach might be needed. The only way I see pulling that off through pure Java GWT methods is via lots and lots of (probably horrible) chaining/invocations of the DOM class. It'd be easier if all you had to do was access some id - for that there's RootPanel.get(id) (and DOM.getElementById(id), they differ in what type of objects they return).

然而,像你已经建议的,JSNI可能提供更好的解决方案 - 尝试返回,例如 $ wnd。$(从JSNI作为一个元素 - 实际上,你可以返回任何东西从JSNI,GWT将只是crap的第二个你尝试使用,说一个int作为一个DOM元素或某物。

However, like you already suggested, JSNI might offer a better solution - try returning, for example, $wnd.$("#someId .className a") from JSNI as an Element - actually, you can return anything as anything from JSNI, GWT will just crap up the second you try to use, say an int as a DOM element or something.

PS:虽然GQuery项目看起来死/非活动,它可能值得检查它们如何包装jQuery调用(例如 $ ),以便它们可以在GWT中使用。

PS: while the GQuery project does seem dead/inactive, it might be worth checking how they wrapped the jQuery calls (such as $) so that they could be used seemingly in GWT.

这篇关于在GWT中通过CSS选择器查找元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆