javascript getElementByID().parentNode 返回 null
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3941939/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
getElementByID().parentNode is returning null
提问by Sachin Shanbhag
For some reason, this is happening very vaguely. Its working sometimes and sometimes it is not. The same line of code, for the different "myid" under same parent, the line -
出于某种原因,这发生得非常模糊。它有时起作用,有时不起作用。同一行代码,对于同一父级下的不同“myid”,该行 -
document.getElementById("myid").parentNodeis returning null.
document.getElementById("myid").parentNode正在返回空值。
I am quite sure that element "myid" is not a root element and its parent is a DIV which needs to be returned. I am using Firefox 3.6.10 version.
我很确定元素“myid”不是根元素,其父元素是需要返回的 DIV。我使用的是 Firefox 3.6.10 版本。
Can anyone suggest any reason why this could be happening?
任何人都可以提出任何可能发生这种情况的原因吗?
EDIT: The "myid" here is some kind of a textbox or any other control element. But the parentnode is always DIV. Any controls we add are always wrapped under a DIV. So basically when something on the screen refreshes, we get the parent node and replace the innerhtml. The innerhtml could be anything.
编辑:这里的“myid”是某种文本框或任何其他控制元素。但是父节点始终是 DIV。我们添加的任何控件总是包含在一个 DIV 下。所以基本上当屏幕上的东西刷新时,我们得到父节点并替换innerhtml。innerhtml 可以是任何东西。
given below is the html I have -
下面给出的是我拥有的 html -
<div style="height: 334px; width: 769px; position: relative;">
<span style="display: inline-block; height: 13px; width: 61px; position: absolute; left: 393px; top: 84px;" bizappid="System856UserGroupAppPoint156d5elabel300" tabindex="-1" id="System856UserGroupAppPoint156d5elabel300">User Group</span>
<input type="text" style="height: 20px; width: 221px; position: absolute; left: 503px; top: 77px;" bizappid="System856UserGroupAppPoint156d5etextBox190" class="formtextbox" tabindex="400" id="System856UserGroupAppPoint156d5etextBox190" readonly="readonly" name="System856UserGroupAppPoint156d5etextBox190">
</div>
In this Html, assume I am getting ParentNode for Span element, but I am not getting the same parentNode for the Input text element. Also one more strange thing is, I just added a check saying if getelementbyid is not null, then check its parentNode. Then further added if parentNode is not null then do refresh operation. Now the control is not coming inside the parentNode not null condition.
在此 Html 中,假设我为 Span 元素获取了 ParentNode,但我没有为 Input 文本元素获取相同的 parentNode。还有一件更奇怪的事情是,我只是添加了一个检查,说如果 getelementbyid 不为空,然后检查它的 parentNode。然后进一步添加,如果 parentNode 不为空,则进行刷新操作。现在控件没有进入 parentNode 非空条件。
回答by Pointy
If the same "id" value is shared by two or more elements on your page, then getElementById(may) return a node list instead of a DOM reference. A node list instance has no "parentNode" property.
如果页面上的两个或多个元素共享相同的“id”值,则getElementById(可能)返回节点列表而不是 DOM 引用。节点列表实例没有“parentNode”属性。
Do not re-use "id" values for more than one element is the moral of this story.
不要为多个元素重复使用“id”值是这个故事的寓意。
回答by Cauly
should it be document.getElementById("myid").parentNode?
应该是document.getElementById("myid").parentNode吗?

