this
以下の 3 つのキーワードの使用方法が機能しない理由を理解しようとしています。
HTMLは次のとおりです。
<ul>
<li><a id="i1" href="#">Item 1</a></li>
<li><a id="i2" href="#">Item 2</a></li>
<li><a id="i3" href="#">Item 3</a></li>
</ul>
そして、ここにjQueryがあります:
// Output a list of href ids and append them to the ul element
$('ul li a').each(function() {
$('<li>' + this.id + '</li>').appendTo('ul') // this works
// $(this.id).appendTo('ul') // this doesn't work
// (this.id).appendTo('ul') // this doesn't work
// $(this).attr('id').appendTo('ul') // this doesn't work
});
ここにもjsFiddleがあります
コメントアウトされた 3 つのコード スニペットがどれも機能しない理由を誰か説明できますか?