2014-01-23 5 views
1

콘텐츠를 표시하기 위해 sectionarticle이 있습니다. 내 코드입니다 : 내가 비교할 수 있도록 articles의 id의를 얻기 위해 노력하고 있어요,하지만 난 정의되지 않은 얻고HTML5의 id 비교

<section id = "examples"> 

<article id="item_1"> 
    ... 
</article> 

<article id="item_2"> 
    ... 
</article> 

<article id="item_3"> 
    ... 
</article> 

<article id="item_4"> 
    ... 
</article> 

... 

</section> 

. 내 코드는 다음과 같습니다

var compare = "item_1" 

$('article').each(function(){ 
var pos = $('article').id; 
console.log(pos) //getting undefined 

    if(pos == compare) 
    // do something 

}); 

답변

2

사용 this 얻을 각 문서 요소의 id : var pos = this.id; :

EXAMPLE HERE

var compare = "item_1"; 

$('article').each(function() { 
    var pos = this.id; 
    console.log(pos); 

    if (pos == compare) { 
     alert('match'); 
    } 

}); 

출력 :

item_1 
item_2 
item_3 
item_4