javascript 找到一个嵌套的 div 标签

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13063801/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 17:44:56  来源:igfitidea点击:

find a nested div tag

javascripthtmldom

提问by MJ_Developer

I have two div tags like bellow:

我有两个 div 标签,如下所示:

<body>
   <div id="divParent">
      <div id="divChild"></div>
   </div>
</body>

How can I get the divChild using JavaScript?

如何使用 JavaScript 获取 divChild?

回答by vdbuilder

Use this:

用这个:

divChild = document.getElementById("divChild");

or if you didn't have the child id:

或者如果您没有孩子的 ID:

divChild = document.getElementById("divParent").children[0];

Edit:

编辑:

you can hide or show child with:

您可以隐藏或显示孩子:

divChild.style.display = "none";//hide
divChild.style.display = "block";//show

回答by Priyank Patel

 var myDiv = document.getElementById('divChild');

Then you can get the contents like this

然后你可以得到这样的内容

 var content = myDiv.innerHTML;

回答by daniel

var div = document.getElementById("divChild");

回答by Steve

var el = document.getElementById("divChild")

var el = document.getElementById("divChild")