html结构为:<nav class="nav-class"> <ul class="ul-class"> <li class="li-class"><a class="a-class">链接1</a></li> <li class="li-class"><a class="a-class">链接2</a></li> <li class="li-class"><a class="a-class">链接3</a></li> <li class="li-class"><a class="a-class">链接4</a></li> <li class="li-class"> <a class="a-class">链接5</a> <ul> <li class="li-class"><a class="a-class">链接6</a></li> <li class="li-class"><a class="a-class">链接7</a></li> </ul> </li> </ul></nav>JS获取 nav-class 下的 a 标签:var urlLink = document.getElementsByClassName('nav-class').getElementsByTagName('a'), find = false;提示错误,这样的应该如何获取?谢谢
1 回答
阿晨1998
TA贡献2037条经验 获得超6个赞
先给代码:
var urlLink = document.getElementsByClassName('nav-class').item(0).getElementsByTagName('a').item(0);
不管是Document还是Element对象,你调getElements~返回的就是个集合,所以你要想不这么麻烦,要不用id要不就上个querySelectorAll得了……
添加回答
举报
0/150
提交
取消