ページに追加されたときに、閉じるボタンを div に動的に挿入する関数があります。
//Insert Close ALERT WINDOW button
function addCloseAlertButton(){
if ($(".alert").find('.closeAlerts').length < 1){
$(".alert").append('<div class="closeAlerts" onclick="closeAlert()">Dismiss</div>');
}
};
そして onclick によって呼び出される関数:
//Close Alert Function
function closeAlert(){
$(this).parent().remove();
};
しかし、div をクリックしても、期待どおりにアラート div が削除されません。私console.log($(this))
が関数に入ったとき、それ$(this)
が全体を参照していwindow
て$(this).parent
空になったので、関数が機能していないことがわかりました。
なぜこれが起こっているのか、どうすればそれを作成できるのか$(this)
、全体ではなく呼び出し側の div を参照する方法を知っている人はいますwindow
か?