私は次のように3つのdiv
要素を持っています
<div id='branch-1'>
<ul>
<li><span>Parent-1</span></li>
<li><span>Parent-2</span></li>
<li><span>Parent-3</span></li>
</ul>
</div>
<div id='branch-2'></div>
<div id='branch-3'></div>
div
ユーザーがクリックすると、最初の要素がページとともに読み込まれ、次のように使用して、同様にParent-1
、その子要素が 2 番目に読み込まれます。div
Parent-2 and Parent-3
ajax
$("div#branch-1 ul li span").click(function () {
$.ajax({
type: 'POST',
url: url,
data: data,
dataType: 'html',
async: false,
success: function (data) {
$("div#branch-2").html(data);
$("div#branch-3").empty();
}
});
});
ユーザーが 2 番目div
の要素をクリックすると、次のように子要素を 3 番目にロードしdiv
ますajax
。
$(document).on("click", "div#branch-2 ul li span"function () {
$.ajax({
type: 'POST',
url: url,
data: data,
dataType: 'html',
async: false,
success: function (data) {
$("div#branch-3").html(data);
}
});
});
実際の質問:既存のデータが消去されないこと$("div#branch-3").html(data);
があります。そのため、以前に次のステートメントを試しました$("div#branch-3").html(data);
$("div#branch-3").html(""); // Not worked
//OR
$("div#branch-3").empty(); // Not worked
//OR
$("div#branch-3").contents().remove(); // Not worked
上記selector
をdiv#branch-3
todiv#branch-3 ul
またはdiv#branch-3 li
or div#branch-3 span
then に変更すると、上記のすべてのステートメントが正常に機能します。
html(data)
既存のデータを置き換えることができないのはなぜですか?- として
selector
渡しているのに、既存の要素をクリアする理由を変更した後parent element id
selector
追加情報を追加:上で述べたように、既存のデータが消去されない場合があります。 .html(data)
Parent-1
2 つの子があるとします。ここChild-1,Child-2
でもChild-1
2 つの子がA,B
ありChild-2
、3 つの子があります。X,Y,Z
ユーザーがクリックしたChild-2
場合X,Y,Z
はbranch-3 div
その罰金で表示され、再びユーザーがクリックしたChild-1
場合X,Y
は置き換えられますA,B
が、Z
まだ存在します。firebug でその (Z) の場所を指定しようとすると、消えてしまいます。