简单的 Javascript XML 幻灯片
我似乎无法弄清楚为什么这不起作用。我只有两个简单的函数:一个将图像加载到 img 节点中,另一个加上 i 变量。第二个函数在计时器事件+计数后触发,然后再次触发 image() 函数。除非它不起作用。
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
//XML Loaded, create the slideshow
alert(xmlhttp.responseText);
var slideShow = document.getElementById('slideShow');
var items = [];
var nl = xmlhttp.responseXML.getElementsByTagName('image');
var i = 0;
var t;
function image() {
var slideShowImg = document.getElementById('slideShowImg');
var nli = nl.item(i);
var src = nli.getAttribute('src').toString();
var width = parseInt(nli.getAttribute('width').toString());
var height = parseInt(nli.getAttribute('height').toString());
var imgNode = document.createElement('img');
imgNode.setAttribute('src', src);
imgNode.setAttribute('height', height);
imgNode.setAttribute('width', width);
slideShowImg.appendChild(imgNode);
t = setTimeout("nextImage()", 5000);
}
function nextImage() {
i++;
image();
}
image();
}
I can't seem to figure out why this isn't working. I just have two simple functions: one that loads the image into an img node, and one that pluses an i variable. The second function fires after a timer event, + the count, and then fires the image() function again. Except it's not working.
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
//XML Loaded, create the slideshow
alert(xmlhttp.responseText);
var slideShow = document.getElementById('slideShow');
var items = [];
var nl = xmlhttp.responseXML.getElementsByTagName('image');
var i = 0;
var t;
function image() {
var slideShowImg = document.getElementById('slideShowImg');
var nli = nl.item(i);
var src = nli.getAttribute('src').toString();
var width = parseInt(nli.getAttribute('width').toString());
var height = parseInt(nli.getAttribute('height').toString());
var imgNode = document.createElement('img');
imgNode.setAttribute('src', src);
imgNode.setAttribute('height', height);
imgNode.setAttribute('width', width);
slideShowImg.appendChild(imgNode);
t = setTimeout("nextImage()", 5000);
}
function nextImage() {
i++;
image();
}
image();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这行看起来很可疑:
试试这个:
This line looks very suspicious:
Try this instead:
将函数定义移出 if 语句。
Move the function definitions out of the if statement.