#content と #contentSwap の 2 つの div があります。一部のデータを #contentSwap にロードし、いくつかの操作を実行してから、#content と交換しています。html() 関数を使用して 2 つを交換していましたが、これが問題を引き起こしていることに気付きました: click() を使用して #contentSwap でアンカーに割り当てていた関数が失われていました。コードの例を次に示します。
$('#contentSwap').html(data);
$('#contentSwap a').each(function(){
$(this).click(function(){
// Do whatever
});
});
$('#content').html($('#contentSwap').html());
$('#contentSwap').html("");
これ以降、#content では click() で割り当てた関数が動作しません。
ただし、attr() 関数を使用して各 div の id を変更すると、click() 関数が固執します。しかし、これは毎回うまくいくわけではなく、#content や #contentSwap などの複数のコピーを含むあらゆる種類のバグを引き起こします。そのためのコードは次のとおりです。
$('#content').attr("id", "contentSwap1");
$('#contentSwap').attr("id", "content");
$('#contentSwap1').attr("id", "contentSwap");
$('#contentSwap').html("");
割り当てた click() 関数を失わずに #content と #contentSwap を効果的に交換するにはどうすればよいですか?