2012-04-18 5 views
0

이 코드는 아직 완전히 완성되지 않았습니다. 나는 페이지 내에 더 이상 'm'이 없을 때까지 페이지 내의 모든 'm'변수를 반복하려고합니다. 지금 당장 나는 틀린 것으로 알고있다. 누구든지 정확한 구문에 대한 올바른 방향으로 나를 가리킬 수 있습니까?문서의 변수 (자바 스크립트)

var m = document.getElementsByClassName('project') 
    var n = document.getElementsByClassName('web') 
    var o = document.getElementsByClassName('print') 
    var p = document.getElementsByClassName('illustration') 

function projectFilter(type){ 
    if (type === 'print'){ 
     for (m in document){ 
     if (getElementsByClassName('print') != null){ 
      m.style(opacity=0.3) 
      console.log("Whatshappening") 
     } 
    } 
    if (type === 'web'){ 
     console.log('webbyshit') 
    } 
    if (type === 'illustration'){ 
     console.log('illustrating') 
    } 
    if (type === 'project'){ 
     console.log('EVERYTHING') 
    } 
} 

고마워요!

답변

3

이미 스크립트 상단에 m이 지정되어 있으므로 in document을 찾을 필요가 없습니다. 그냥 루프는 당신이 루프 배열을 (mNodeList 목적이지만 배열과 같은 length 재산 할 수있다) 것 같은 :

for(var i=0; i<m.length; i++) { 
    // do something with each m[i] 
} 
1

세 가지 문제

  • 잘못
  • 을 반복하는 당신의 스타일 할당이 올바르지 않습니다.
  • getElementsByClassName은 빈 세트 인 경우에도 항상 객체를 반환합니다. 길이 속성을 확인하여 요소를 다시 가져 왔는지 확인하십시오.

 

for (var i = 0, len = m.length; i<len; i++) { 
    if (m[i].getElementsByClassName('print').length > 0){ 
     m.style.opacity=0.3; 
    } 
}