クラス「角丸」を持つ現在のページ上のすべてのdivを検索し、それらのdivをいくつかのきれいな背景画像を含むラッピングテーブルに置き換えるjQuery関数を作成しようとしています。下記参照...
$(document).ready(function ()
{
// For all "rounded-corners" div's on the page...
$("DIV.rounded-corners").each(function ()
{
// Copy the contents of the div into a wrapping table.
var $roundedCornersContainer = $('<table cellpadding="0" cellspacing="0" class="rounded-corners-container"> \
<tr> \
<td class="corner-topleft"></td> \
<td class="corner-topright"></td> \
</tr> \
<tr> \
<td class="original-content" colspan="2">THIS IS WHERE THE CONTENT IS INSERTED</td> \
</tr> \
<tr> \
<td class="corner-bottomleft"></td> \
<td class="corner-bottomright"></td> \
</tr> \
</table>');
$roundedCornersContainer.find("TD.original-content").append($(this).html());
// Replace the original "rounded-corners" div with the populated wrapping table.
$(this).replaceWith($roundedCornersContainer);
});
});
これは、ネストされた「角の丸い」div がない限りうまく機能します。ある場合は、最も外側の divのみがラッピング テーブルに置き換えられます。
興味深いことに、デバッガーを使用してこのコードを実行すると、ネストされたすべての div が実際に取得されて処理されます。画面上で更新されることはありません。(注: 最も外側の div が最初に処理され、次にネストされた各子が処理されます)。
- each() 関数の DOM 要素が古くなっているのでしょうか?
- 各反復の最後に何らかの形で DOM を明示的に更新する必要がありますか?
- または、すべての div を一度に処理する方法はありますか (つまり、each() 関数を使用しない)?