JSP、Javascript、在另一个 Id 中获取 ElementId

发布于 2024-11-27 20:48:59 字数 394 浏览 5 评论 0原文

我今天的问题围绕着 Websphere JSP 环境中的 javascript 世界...

我有一个有点像这样的代码:

<div id="randomDynamicId">
    <input id="whatIwantToGetTo">
</div>

我知道我可以直接查找该 id,但这是在 Websphere 门户中,我“应该" 可以通过 document.getElementById() 直接链接到它,但我总是需要获取之前的 div 的 id。
(它最终成为带有名称空间的 portlet id,因为有时这些 portlet 可能会被复制,所以我只想专门针对一个)

我可以通过什么方式来做到这一点?
提前致谢。

my question today revolves arround the world of javascript in a Websphere JSP environment...

I have a code that is somewhat like this:

<div id="randomDynamicId">
    <input id="whatIwantToGetTo">
</div>

I know that I could just look for that id directly, but this is in a Websphere portal, I "should" be able to link to it directly by document.getElementById(), but I always need to acquire the id of the prior div.
(it ends up being the portlet id with the namespace and since sometimes these portlets might be replicated I want to target just one specifically)

Any way that I might be able to do this?

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

情绪少女 2024-12-04 20:48:59

根据您的要求,我发布了我之前的评论作为答案,更详细一点:如果问题是 whatIwantToGetTo 没有命名空间前缀,那么您最终会在您的页面,您还应该将 JSP 重写为所有 id 属性的命名空间。无论如何,这可能应该完成(如果您可以修改 HTML,也就是说),至少如果 portlet 有可能在任何页面上出现多次!

然而,当您使用 WebSphere Portal 7 时,您很可能拥有 Dojo,并且您可以利用它的 CSS 样式选择器机制,如下所示:

var inputElement = dojo.query('#randomId > input');

As per your request I post my earlier comment as an answer, a little more elaborated: If the issue is that whatIwantToGetTo is not namespace-prefixed so that you end up with multiple elements with the same id on your page, you should rewrite your JSP to namespace all id attributes as well. This should probably be done anyway (if you can modify the HTML, that is), at the very least if there is a possibility of the portlet occurring more than once on any page!

However, seeing as you're on a WebSphere Portal 7, you most likely have Dojo around and you could leverage its CSS-style selector mechanism like so:

var inputElement = dojo.query('#randomId > input');
不顾 2024-12-04 20:48:59

你想要的是

document.getElementById ('whatIwantToGetTo').parentNode

What you want is

document.getElementById ('whatIwantToGetTo').parentNode
滥情空心 2024-12-04 20:48:59

如果您可以使用 jQuery,则可以像这样完成此操作:

var parent = $('#whatIwantToGetTo').parent();

参见此处

纯 JavaScript 替代方案类似于这:
警报(document.getElementById('objectIWant').parentNode.id);

参见此处

If you can use jQuery, you can accomplish this like so:

var parent = $('#whatIwantToGetTo').parent();

See Here

The pure javascript alternative is something like this:
alert(document.getElementById('objectIWant').parentNode.id);

See here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文