ボタンにライブクリック機能を使用して、Ajax Loaded HTML で動作するようにしたいのですが、jQuery のライブクリックの新しいバージョン -- $(".button").live("click", function() {...})
-- が.on()
機能を備えたものであることを知りました。
その構文では、要素はパラメーター内にあるため、要素が「this」の場合、その要素を取得できませんでした。それを取り戻す方法はありますか?
$(".selection-item a").each(function(){
var parent_selection = $(this).parents(".selection-item-wrapper");
// $(this).click(function(){ //This is the original function which works fine EXCEPT on Ajax Loaded HTML
$(parent_selection).on("click",this,function(){
$(".selection-item a",parent_selection).removeClass("active");
$(this).addClass("active"); //This does not target my desired element which is $(".selection-item a")'s this
console.log( $(this) ); // The "this" value returns the parent_selection, I want it to return the particular "this" of $(".selection-item a") read from the .each() on top of my code
});
});
これは私の HTML です。シンプルなアイテムセレクションを作成しています。画像のアンカー タグをクリックするたびに、「アクティブな」クラスを追加して、特定の項目が選択されたことを示す境界線付きの css を追加します。
<div class="selection-item-wrapper">
<div class="selection-item">
<a href="javascript:void(0);"><img src="img/image1.png"></a>
<h3>Item 1</h3>
</div>
<div class="selection-item">
<a href="javascript:void(0);"><img src="img/image2.png"></a>
<h3>Item 2</h3>
</div>
<div class="selection-item">
<a href="javascript:void(0);"><img src="img/image3.png"></a>
<h3>Item 3</h3>
</div>
</div>