我正在嘗試選擇元素的childNode
。 這是我在嘗試這樣做:JS-從FireFox中選擇childNodes的問題
var postBody_elem = elem.parentNode.parentNode.childNodes['post'].childNodes['postBody'];
我也曾嘗試:
elem.parentNode.parentNode.childNodes[0].childNodes[1];
我知道它是與childNodes
的問題,而不是parentNodes
,我有測試。
這是我在做什麼的簡化結構:
<section id = 'postWrapper'> //This is the Second Parent.
<article id = 'post'> //This should be the First Child.
<section id = 'postInfo'>
<section id = 'postTitle'></section>
<section id = 'poster'></section>
<section id = 'postDate'></section>
</section>
<section id = 'postBody'> //This should be the Second Child.
</section>
</article>
<section id = 'postBar'> //This is the First Parent.
<img id = 'portrait'>
<section id = 'editButton'>Edit</section> //This is the var elem.
<section id = 'deleteButton'>Delete</section>
</section>
</section>
我之所以通過父和子節點導航到引用'postBody'
是因爲這種格式被重複多次。
- 這使我想到我的問題,上面的代碼行工作在谷歌瀏覽器,但在Firefox不工作,我已經嘗試了許多變化,但控制檯給我的錯誤,這是不確定的。 我已經檢查過元素是否正確陳述,這只是瀏覽器處理它的方式之間的差異,我認爲。
如果任何人有答案,我希望它是關於如何可以引用我已經顯示的元素,而不是關於如何不正確地使用id
多個元素的講座,因爲這不是一個問題在這案例(據我所知,因爲它在Chrome中工作)。
謝謝。
它重複了很多次。 ..你正在使用「獨特」ID? – Neil
@尼爾是的,是的,我是。我知道爲什麼嗎?不,但我喜歡。 – MichaelMitchell
對不起教你,我忽略了你最後一句話。 – Neil