这是一个愚蠢的小问题,我知道,但我似乎无法找到答案...
我尝试使用带有名称的数组更改Jquery中的图像,但它读取的是n,g和2而不是实际值。
我的代码是:
var huidig = 1;
var arrayTekenen = jQuery.makeArray("peng1.png","peng2.png","peng3.png","peng4.png");
$(document).ready(
function() {
$('#next').bind('click', ClickNext);
}
)
function ClickNext() {
if(huidig<4)
{
huidig++;
}
$("#tekenstap").attr('src', 'img/' + arrayTekenen[huidig]);
}
这是我点击对象时遇到的错误:
无法加载资源(15:27:28:901 |错误,网络) http:// localhost:8383 / HTML5Application / img / n无法加载资源 (15:27:30:048 |错误,网络)at http:// localhost:8383 / HTML5Application / img / g无法加载资源 (15:27:30:729 |错误,网络)at http:// localhost:8383 / HTML5Application / img / 2
提前致谢。
答案 0 :(得分:4)
jQuery.makeArray没有按照您的想法行事。
不是在那里创建一个数组。它将类似数组转换为实数数组,并且只接受一个参数:类似数组。
如果要创建数组,请使用以下语法:
var arrayTekenen = ["peng1.png","peng2.png","peng3.png","peng4.png"];
您应该阅读的一些资源:MDN Array。
现在你问我:什么是类似数组的对象?它是一个看起来像数组的对象,像数组一样嘎嘎叫,像数组一样行走,但不是数组。示例包括arguments,NodeLists或HTMLCollection。主要是,它们可以用作数组,但没有slice
,splice
,forEach
等数组方法。
$.makeArray
的作用基本上是这样的:
function makeArray(obj) {
return Array.prototype.slice.call(obj);
}
我留给你了解它是如何工作的: - )
答案 1 :(得分:2)
这不是jQuery.makeArray
的工作原理。只需使用数组文字表示法:
var arrayTekenen = ["peng1.png","peng2.png","peng3.png","peng4.png"];
在你的情况下,arrayTekenen结果是一个字符串(“peng2.png”),所以arrayTekenen [i]是一个字符而不是文件名。