カスタム Bootstrap モーダルを挿入する必要がある場合に使用できるスクリプトが必要であると判断しました。空の静的な Bootstrap モーダル HTML が常に使用されるとは限らない場合、すべてのページの下部に配置したくありませんでした。
したがって、これは間違った方法かもしれませんが、これが私の試みでした。モーダル「シェル」html である変数を作成します。次に、デバイス項目をクリックすると、これが本文に追加されます。いくつかのコンテンツを複製して、モーダルのヘッダーと本文に追加しました。すべて正常に動作しています。しかし、一度閉じたモーダルを削除することはできません。これは、モーダル シェル HTML が HTML ページに静的に存在する場合、削除が正常に機能するため、JS を介して HTML を挿入するという事実と関係があります。
HTML:
<ul>
<li class="span4 device">
<div class="inner">
<h3>Device 4</h3>
<div class="device-product">
<img class="device-image" src="img/placeholder-holding-image.png" alt="Holding Image" />
<a href="#" class="hide">Troubleshoot this item</a>
<a href="#" class="hide">How to use this product</a>
</div>
<div class="device-details">
<div class="control-group">
<label class="control-label">Device Type:</label>
<span class="field">Really cool device</span>
</div>
<!-- Small amount of hidden additional information -->
<div class="control-group hide">
<label class="control-label">Device ID:</label>
<span class="field">123456</span>
</div>
</div>
</div>
</li>
</ul>
jQuery:
var customModal = $(
'<div class="custom-modal modal hide fade" tabindex="-1" role="dialog" aria-hidden="true"> \
<div class="modal-header"><button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button></div> \
<div class="modal-body"></div> \
<div class="modal-footer"><button class="btn" data-dismiss="modal">Close</button></div> \
</div>'
);
$('.device').click(function(){
$('body').append(customModal);
$(this).find($('h3')).clone().appendTo('.custom-modal .modal-header');
$(this).find('.device-product, .device-details').clone().appendTo('.custom-modal .modal-body');
$('.custom-modal.hide').show();
$('.custom-modal').modal();
});
$('.custom-modal').on('hidden', function(){
$(this).remove();
});
だから本当に私が苦労しているのは remove() だけです。しかしまた、私が間違った/非効率的な方法でこれを行っているかどうかについてのコメントは、常に学習に役立ちます!