[英]Comparison of id in HTML5
我有用於顯示內容的section
和article
。 我的代碼是:
<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
的ID,以便可以進行比較,但是我不確定。 我的代碼是:
var compare = "item_1"
$('article').each(function(){
var pos = $('article').id;
console.log(pos) //getting undefined
if( pos == compare)
// do something
});
使用this
來獲取每個文章元素的id
: var pos = this.id;
:
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.