JSP、Javascript、在另一个 Id 中获取 ElementId
我今天的问题围绕着 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
根据您的要求,我发布了我之前的评论作为答案,更详细一点:如果问题是
whatIwantToGetTo
没有命名空间前缀,那么您最终会在您的页面,您还应该将 JSP 重写为所有id
属性的命名空间。无论如何,这可能应该完成(如果您可以修改 HTML,也就是说),至少如果 portlet 有可能在任何页面上出现多次!然而,当您使用 WebSphere Portal 7 时,您很可能拥有 Dojo,并且您可以利用它的 CSS 样式选择器机制,如下所示:
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 allid
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:
你想要的是
What you want is
如果您可以使用 jQuery,则可以像这样完成此操作:
参见此处
纯 JavaScript 替代方案类似于这:
警报(document.getElementById('objectIWant').parentNode.id);
参见此处
If you can use jQuery, you can accomplish this like so:
See Here
The pure javascript alternative is something like this:
alert(document.getElementById('objectIWant').parentNode.id);
See here