遇到一个有趣的问题。我在进入我的 html ( class="day"
) 时隐藏了一周中的每一天。通过单击日期,它会通过 AJAX 调用扩展以显示该日期下的所有内容$.post
。这一切都运作良好。
我认为用户可能想要一次查看整个星期,所以我有一个按钮 ( 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 是否发生得太快,但没有任何改变。