2014-01-23 8 views
1

У меня есть section и article для отображения содержимого. Мой код:Сравнение идентификатора в HTML5

<section id = "examples"> 

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

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

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

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

... 

</section> 

Я пытаюсь получить корочки articles, так что я могу сравнить, но я получаю не определено. Мой код:

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