Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
85 views
in Technique[技术] by (71.8m points)

javascript - 获取元素的父div(Getting the parent div of element)

This should be really simple but I'm having trouble with it.(这应该很简单,但我遇到了麻烦。)

How do I get a parent div of a child element?(如何获取子元素的父div?) My HTML:(我的HTML:) <div id="test"> <p id="myParagraph">Testing</p> </div> My JavaScript:(我的JavaScript:) var pDoc = document.getElementById("myParagraph"); var parentDiv = ?????????? I would have thought document.parent or parent.container would work but I keep getting not defined errors.(我原以为document.parentparent.container可以工作,但我一直not defined错误。) Note that the pDoc is defined, just not certain variables of it.(请注意, pDoc是定义的,而不是它的某些变量。) Any ideas?(有任何想法吗?) PS I would prefer to avoid jQuery if possible.(PS我希望尽可能避免使用jQuery。)   ask by OVERTONE translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You're looking for parentNode , which Element inherits from Node :(您正在寻找parentNodeElement继承自Node :)

parentDiv = pDoc.parentNode; Handy References:(方便参考:) DOM2 Core specification - well-supported by all major browsers(DOM2核心规范 - 得到所有主流浏览器的良好支持) DOM2 HTML specification - bindings between the DOM and HTML(DOM2 HTML规范 - DOM和HTML之间的绑定) DOM3 Core specification - some updates, not all supported by all major browsers(DOM3核心规范 - 一些更新,并非所有主流浏览器都支持) HTML5 specification - which now has the DOM/HTML bindings in it(HTML5规范 - 现在包含DOM / HTML绑定)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...