简体   繁体   English

如何从对话框窗口访问javascript中的父元素?

[英]How to access parent element in javascript from a dialog window?

I have a scenario where I have two JSPs. 我有一个场景,其中有两个JSP。 One of the JSPs is called 'parent.jsp' and the other is called 'dialog.jsp'. 一个JSP被称为“ parent.jsp”,另一个被称为“ dialog.jsp”。 On parent.jsp, I have a table with an image on each row. 在parent.jsp上,我有一张表格,每行都有一张图片。 The image is displayed in a div. 图像以div显示。 On clicking the div a dialog pops up (dialog.jsp). 单击div后,将弹出一个对话框(dialog.jsp)。 On dialog.jsp, we have a button called 'Hide'. 在dialog.jsp上,我们有一个名为“隐藏”的按钮。 I want to hide the clicked image on parent.jsp as soon as the button 'Hide' on dialog.jsp is clicked but I cannot access the image div on parent.jsp in the onclick function of button 'Hide' on dialog.jsp. 我想在单击dialog.jsp上的“隐藏”按钮后立即在parent.jsp上隐藏单击的图像,但是我无法在dialog.jsp上的“隐藏”按钮的onclick函数中访问parent.jsp上的图像div。 Here's my code: 这是我的代码:

parent.jsp parent.jsp

<table width="100%" border="0" cellspacing="0" cellpadding="0">
<c:forEach items="${listOfElements}" var="element">

    <div id="myImage" style="background: url('images/myImage.png');" onclick="showDialog()"></div>

</c:forEach>
</table>

dialog.jsp dialog.jsp

<input name="hideButton" type="button" onclick="hideImage();" value="Hide" />
<script type="text/javascript">
    function hideImage() {
        document.getElementById('myImage').hide();
    }
</script>

I get a null value at ' document.getElementById('myImage') '.I have used ' window.parent.document.getElementById('myImage').hide() ' too but the null value is still there. 我在' document.getElementById('myImage') '上获得了一个空值。我也使用了' window.parent.document.getElementById('myImage').hide() ',但该空值仍然存在。 Note that I use Ext.Window to create the dialog. 请注意,我使用Ext.Window创建对话框。

inside showDialog(), I think you are using window.open(). 在showDialog()中,我认为您正在使用window.open()。 window.open will give return newly opened window object. window.open将返回新打开的window对象。 So that you can access newly opened window from parent. 这样您就可以从父级访问新打开的窗口。 In the same way window.opener will give reference to parent window inside popup window. 以同样的方式,window.opener将在弹出窗口中引用父窗口。

Below links will help you 以下链接将为您提供帮助

https://developer.mozilla.org/en-US/docs/Web/API/Window/open https://developer.mozilla.org/zh-CN/docs/Web/API/Window/open

https://developer.mozilla.org/en-US/docs/Web/API/Window/opener https://developer.mozilla.org/zh-CN/docs/Web/API/Window/opener

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

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