A function that obtains the nth element node can now only obtain elements through the html tag, and its function is not perfect.
Demo: html
<ul id="list"><li>1<button>a</button></li><li>2<button>b</button><button>o</button></li><p>test</p><li>3<button>c</button></li><li>4<button>d</button></li><li>5<button>e</button></li></ul>
js:
/**** @param parent parent node* @param ele element label to select* @param num Which element is the first * @return {*}*/function nth(parent,ele,num){var _ele=Array.prototype.slice.call(parent.childNodes),eleArray=[];//Convert the child node of the parent node into an array_ele;eleArray is an array that only stores element nodes for(var i= 0,len=_ele.length;i<len;i++){if(_ele[i].nodeType==1){eleArray.push(_ele[i]);//Filter out non-element nodes}}if(arguments.length===2){//If only 2 parameters are passed in, then if the second parameter is a number, select the number of elements under the parent node.//If the second parameter is a string, select the node represented by all parameters under the parent node if(typeof arguments[1]==="string"){_ele=Array.prototype.slice.call(parent.getElementsByTagName(arguments[1])); return _ele;}else if(typeof arguments[1]==="number"){return eleArray[arguments[1]];}}else{//If the parameters are complete, return to the node, and the index starts from 0_ele=Array.prototype.slice.call(parent.getElementsByTagName(ele));return _ele[num];}}/*test*/var list=document.getElementById("list");console.log(nth(list,"li",2).innerHTML);//Select the third li element console.log(nth(list,"button",3).innerHTML)//Select the fourth button console.log(nth(nth(list,"li",1),"button",1).innerHTML);//Select the second button console.log(nth(nth(list,"li",1),"button"));//Select all button console.log(nth(list,2));//Select the second element