ちょっと面白い問題にぶつかります。HTML()に入ると、曜日が非表示になりますclass="day"
。Dayをクリックすると、展開されて、。を使用したAJAX呼び出しにより、その日のすべてが表示されます$.post
。これはすべてうまく機能しています。
ユーザーが1週間全体を一度に見たいと思うかもしれないと思うので、id="expander"
すべてを表示するボタン()があります。getSchedule()
ただし、クリック内から関数を呼び出すと、関数内#expander
を除くすべてがループします。次に、をループします。$.post
getSchedule()
$.post
$(document).ready(function()
{
$('.day').click(function(){
pass = $(this).prop('id');
getSchedule(pass);
});
$('#expander').click(function(ex){
$('.day').each(function(index){
pass = $(this).prop('id');
getSchedule(pass);
});
$('#expander').text('Hide All Days');
ex.preventDefault();
return false;
});
});
function getSchedule(elementid){
dow = elementid.substr(-1);
url = "standarddayofweek.php?d="+dow;
$theday = $("#"+elementid);
console.log("The day is "+$theday.prop('id'));
$.post(url,function(data){
$theday.children('.dayschedule').html(data);
}, "html"
)
}
私が得るコンソールの応答は次のとおりです。
The day is d1
The day is d2
The day is d3
The day is d4
The day is d5
The day is d6
The day is d7
XHR finished loading: "http://127.0.0.1/standarddayofweek.php?d=1"
In the post function, the day is d7
XHR finished loading: "http://127.0.0.1/standarddayofweek.php?d=2"
In the post function, the day is d7
XHR finished loading: "http://127.0.0.1/standarddayofweek.php?d=3"
In the post function, the day is d7
XHR finished loading: "http://127.0.0.1/standarddayofweek.php?d=4"
In the post function, the day is d7
XHR finished loading: "http://127.0.0.1/standarddayofweek.php?d=5"
In the post function, the day is d7
XHR finished loading: "http://127.0.0.1/standarddayofweek.php?d=6"
In the post function, the day is d7
XHR finished loading: "http://127.0.0.1/standarddayofweek.php?d=7"
In the post function, the day is d7
骨抜きにされたHTML:
<div id="expander">Click me to expand</div>
<div id="d1" class="day">
<div class="dayschedule"></div>
</div>
<div id="d2" class="day">
<div class="dayschedule"></div>
</div>
....
<div id="d7" class="day">
<div class="dayschedule"></div>
</div>
何が起こっている?AJAXの発生が速すぎるかどうかを確認するために、遅延を追加しようとしましたが、何も変わりませんでした。