2013-08-21 3 views
1

많은 하위 항목이 포함 된 html 블록이 있습니다.요소의 모든 하위 항목을 순환하여 해당 속성을 가져옵니다.

<div id="selectedId"> 
    <div class=".."><a class=".."></a> 
     <button style=".."></button> 
    </div> 
    <div id="otherId"> 
     <table></table> 
    </div> 
</div> 

부모를 선택하려면 jquery를 사용하고 있습니다. 그런 다음 모든 자손을 반복하고 클래스 또는 innerHtml과 같은 다양한 속성에 액세스하려고합니다. 나는 그들의 속성에 액세스 할 수없는 것 같습니다.

function rewriteCategoryName(oldCategoryName, newCategoryName) 
{ 
    // get div parent 
    var parentDiv = $("#selectedId"); 

    parentDiv.find('*').each(function(el,key) { 
     alert(el+" --"); 
     el.each(function(el,key) { 
      // i get just numbers here, and undefined on everything 
      // i try to access 
     }); 

    }); 
} 

아이디어가 있으십니까? 여기서 내가 뭘 잘못하고 있니? 제가 누락 된 개념이 있습니까?

+0

http://stackoverflow.com/questions/14645806/get-all-attributes-of-an-element-using-jquery –

+0

'$ ('. oldClassName ')은 무엇입니까? removeClass ('oldClassName '). addClass ('newClassName ');' – jantimon

+0

템플릿을 사용해야 할 수도 있습니다. http://handlebarsjs.com/ – jantimon

답변

1
$("#selectedId").find('*').each(function(el,key) { 

    $(this).each(function() { 
     $.each(this.attributes, function() { 
     // this.attributes is not a plain object, but an array 
     // of attribute nodes, which contain both the name and value 
     if(this.specified) { 
      console.log(this.name, this.value); 
     } 
     }); 
    }); 

}); 
1

해당 키가 제 파라미터의 각 기능의 순서를 전환 한 상기 소자는 상기 제이므로 변경 :

$("#selectedId").find('*').each(function(el,key) 

에 :

$("#selectedId").find('*').each(function(key,el) { 

자세한 내용은 http://api.jquery.com/jQuery.each/

+0

네, 알아 챘습니다. 고마워요! –

관련 문제