This article describes the method of obtaining all descendant elements under specified elements in native javascript. It is shared with you for your reference. The specific implementation method is as follows:
The commonly used loop recursion method in the past seemed very troublesome. Let me share a relatively simple method to implement this function using the native javascript method.
The code example is as follows:
Copy the code as follows: <!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="author" content="//www.VeVB.COM/" />
<title>javascript gets descendant elements</title>
<script type="text/javascript">
window.onload=function(){
var obox=document.getElementById("box");
var show=document.getElementById("show");
var nodes=obox.getElementsByTagName("*");
show.innerHTML=nodes.length;
}
</script>
</head>
<body>
<div id="show"></div>
<div id="box">
<div>
<ul>
<li>Element One</li>
<li>Element Two</li>
<li>Element Three</li>
</ul>
</div>
</div>
</body>
</html>
The above code implements our requirement, the parameter asterisk represents a wildcard that can match all types of tags.
The call object of the getElementsByTagName() method determines its search range.
I hope this article will be helpful to everyone's JavaScript web programming.