将加载 gif 添加到简单脚本中
我对 Javascript 真的很陌生,但我已经有了这个加载 url 内容的脚本,一切都工作正常。我使用按钮上的 onClick 方法调用 plannerSpin 函数,但是当这一切发生时,我将如何显示动画 gif?
var xmlHttp
function plannerSpin(str) {
xmlHttp = GetXmlHttpObject()
if (xmlHttp == null) {
alert("Browser does not support HTTP Request")
return
}
var url = "/recipes/planner/data"
xmlHttp.onreadystatechange = stateChanged
xmlHttp.open("GET", url, true)
xmlHttp.send(null)
}
function stateChanged() {
if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
document.getElementById("recipe_planner_container").innerHTML = xmlHttp.responseText
}
}
function GetXmlHttpObject() {
var xmlHttp = null;
try {
// Firefox, Opera 8.0+, Safari
xmlHttp = new XMLHttpRequest();
} catch (e) {
// Internet Explorer
try {
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlHttp;
}
Im really really new to Javascript but Ive got this script that loads the contents of a url and everything works fine. I call the plannerSpin function with an onClick method on a button but how would I go about displaying an animated gif whilst all this is going on?
var xmlHttp
function plannerSpin(str) {
xmlHttp = GetXmlHttpObject()
if (xmlHttp == null) {
alert("Browser does not support HTTP Request")
return
}
var url = "/recipes/planner/data"
xmlHttp.onreadystatechange = stateChanged
xmlHttp.open("GET", url, true)
xmlHttp.send(null)
}
function stateChanged() {
if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
document.getElementById("recipe_planner_container").innerHTML = xmlHttp.responseText
}
}
function GetXmlHttpObject() {
var xmlHttp = null;
try {
// Firefox, Opera 8.0+, Safari
xmlHttp = new XMLHttpRequest();
} catch (e) {
// Internet Explorer
try {
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlHttp;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有很多方法...例如:您可以隐藏图像:
并在启动 AJAX 请求时立即显示它:
然后,一旦请求完成,您可以再次隐藏图像:
或者,您可以使用 jQuery 、Prototype、Mootools 或任何您想要的 JS 库。
再见!
There are so many ways... for example: you can have the image hidden:
and show it as soon as you start the AJAX request:
Then, you hide the image again once the request has been completed:
Or, you can use jQuery, Prototype, Mootools, or whatever JS library you want.
Bye!
您可以在 plannerSpin 的开头添加加载 gif 并在 stateChanged 函数中删除。像这样的东西:
You can add the loading gif at the beginning of plannerSpin and remove in the stateChanged function. Somethig like: