自分の良さを説明できればと思います。元の関数の呼び出し後に作成される要素のイベントで実行される jQuery 関数である引数を持つ関数を作成したいと思います (ダイアログに非常に似ています)。これが私がやろうとしていることのコードです:
function messageBox(messageOptions) {
var default_args = {
'header': null,
'text': null,
'acceptButtonText': 'ok',
'acceptButtonOnClickFunction': null,
'cancelButtonText': 'cancel',
'cancelButtonOnClickFunction': function() {
$('#cancel_message_box_div_button_span').click(function() {
$('.greyBackground').remove();
})
}
};
for (var index in default_args) {
if (typeof messageOptions[index] == "undefined") messageOptions[index] = default_args[index];
}
putGrayBackground();
var messageBoxElement = '<div class="messageBoxDiv">';
messageBoxElement += '<legend class="messageBoxDivHeader">';
messageBoxElement += messageOptions.header;
messageBoxElement += '</legend>';
messageBoxElement += '<div class="messageBoxDivText">';
messageBoxElement += messageOptions.text;
messageBoxElement += '</div>';
messageBoxElement += '<div class="messageBoxDivButtons">';
messageBoxElement += '<div id="cancel_message_box_div_button_span" class="messageBoxDivButtonSpan">';
messageBoxElement += messageOptions.cancelButtonText;
messageBoxElement += '</div>';
messageBoxElement += '</div>';
messageBoxElement += '</div>';
$('.greyBackground').append(messageBoxElement);
}
コードは、クリックされたときに「cancelButtonOnClickFunction」引数の下で関数を呼び出すスパンを持つ要素を作成することを想定しています。ご覧のとおり、デフォルトのパラメータがあり、そのうちの 1 つが「cancelButtonOnClickFunction」です。追加する要素を作成した後、要素が作成される前に「cancelButtonOnClickFunction」内部関数が呼び出されるため、機能しないと思います。私はそれを機能させる方法を尋ねます。