获得元素的父div(javascript) [英] Getting parent div of element (javascript)

查看:104
本文介绍了获得元素的父div(javascript)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这应该很简单,但我遇到了麻烦。如何从子元素中获取父div。

This should be really simple but I'm having trouble with it. How do I get a parent div from a child element.

例如

<div id="test>
    <p id="myParagraph">Testing</p>
</div>



Javascript


b $ b

Javascript

var pDoc = document.getElementById("myParagraph");
var parentDiv = ??????????   

我会认为 document.parent parent.container 会工作,但我会收到未定义的错误注意 pDoc

I would have thought document.parent or parent.container would work but I keep getting not defined errors. Note that the pDoc is defined, just not certain variables of it.

任何想法?

PS我希望尽可能避免JQuery。

P.S. I would prefer to avoid JQuery if possible.

推荐答案

您正在寻找 parentNode ,其中 元素 继承自 节点

You're looking for parentNode, which Element inherits from Node:

parentDiv = pDoc.parentNode;

方便的参考资料:

  • DOM2 Core specification - well-supported by all major browsers
  • DOM2 HTML specification - bindings between the DOM and HTML
  • DOM3 Core specification - some updates, not all supported by all major browsers
  • HTML5 specification - which now has the DOM/HTML bindings in it

这篇关于获得元素的父div(javascript)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆