2017-05-11 105 views
0

我想訪問子元素linav的元素。但其顯示不確定。如何訪問子元素<nav>

<nav> 
       <ul class="info-section"> 
        <li><a href="#" data-target="basicInfo">Basic Information</a></li> 
        <li><a href="#" data-target="extInfo">Extended Information</a></li> 
        <li><a href="#" data-target="loanSize">Loan Size/LVR</a></li> 
        <li><a href="#" data-target="loanFees">Loan Fees</a></li> 
        <li><a href="#" data-target="services">Services</a></li> 
       </ul> 
      </nav> 

我正在使用cutome指令

 nav = angular.element(document.querySelector(".info-section")); 

其返回DOM元素戰時代碼。但如果我寫波紋線。其打印件undefine

console.log(nav.children[0].classList); 
+0

什麼是你想用它做?這是在控制器還是服務內? – rrd

+0

在custome指令內 –

回答

1

訪問元素的0位置

nav[0].children[0].classList

演示

angular.module("app",[]) 
 
.controller("ctrl",function($scope){ 
 

 
var nav = angular.element(document.querySelector(".info-section")); 
 
console.log(nav[0].children[0].classList) 
 
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
<div ng-app="app" ng-controller="ctrl"> 
 
<nav> 
 
       <ul class="info-section"> 
 
        <li><a href="#" data-target="basicInfo">Basic Information</a></li> 
 
        <li><a href="#" data-target="extInfo">Extended Information</a></li> 
 
        <li><a href="#" data-target="loanSize">Loan Size/LVR</a></li> 
 
        <li><a href="#" data-target="loanFees">Loan Fees</a></li> 
 
        <li><a href="#" data-target="services">Services</a></li> 
 
       </ul> 
 
      </nav> 
 
</div>

1

你可以這樣使用。沒有直接的方法來獲取元素的兒童

nav = angular.element(document.querySelector(".info-section")); 
nav[0].getElementsByTagName('li') 
0

我有角零知識,但你可以試試這個

var nav = document.getElementsByTagName("nav"); 
 
var ulWithInfoSection = nav[0].getElementsByClassName("info-section")[0]; 
 
console.log(ulWithInfoSection.getElementsByTagName("li"));
<nav> 
 
       <ul class="info-section"> 
 
        <li><a href="#" data-target="basicInfo">Basic Information</a></li> 
 
        <li><a href="#" data-target="extInfo">Extended Information</a></li> 
 
        <li><a href="#" data-target="loanSize">Loan Size/LVR</a></li> 
 
        <li><a href="#" data-target="loanFees">Loan Fees</a></li> 
 
        <li><a href="#" data-target="services">Services</a></li> 
 
       </ul> 
 
      </nav>