我试图创建一个基本的Hangman程序。它是一个被分配到对象数组中的标签数组(称为按钮)。每个图像都是一个字母的图像,例如,您可以按'L'按钮,它会检查这是否在WordArray(这是一个字符数组)中,然后它会采取相应的行动(隐藏字母,然后更新绞架图像,如果需要的话)。从Javascript对象(Javascript)中访问函数
我无法获取图像的onclick方法来访问checkinarray函数。我究竟做错了什么?
var LetterAmount = 3; //make this 26. checked.
var WordArray = new Array();
var Buttons = new Array();
function checkinarray(num){
var z = 0;
while (z<4){
document.write("looping through word now");
if (num == WordArray[z]){
document.write("<br/>it is in the word");
}
z++;
}
}
function ButtonClicked(){
this.Image.src = "images/blank.jpg";
checkinarray(this.Number);
}
function Button(Image, Number) {
this.Image = Image;
this.Number = Number;
this.ButtonClicked = ButtonClicked;
}
function initialiseletters(){
var x;
//set up empty img tags for use
for(i = 0; i < LetterAmount; i++) {
document.write("<img id=" + i + ">");
}
for(x = 0; x < LetterAmount; x++) {
document.images[x].src = "images/" + x + ".jpg";
document.getElementById(x).onclick =function(){
Buttons[this.id].ButtonClicked();
}
Buttons[x] = new Button(document.images[x], "" + x);
}
}
function initialiseword(){
//WordArray = new Array();
WordArray[0] = 0;
WordArray[1] = 1;
WordArray[2] = 2;
WordArray[3] = 3;
}
function initialise(){
initialiseword();
initialiseletters();
document.write("testing overall");
}
感谢响应快! 这是一门课程,他们指定我们不能使用任何工具包或扩展,但是。 – David2334 2009-12-08 17:35:58
然后,你被“原始”DOM方法卡住了:'Document#createElement','Node#appendChild'等,加上有用的,非常广泛支持的和非标准的Element#innerHTML属性(因为在页面被渲染后,你再也不能使用'document.write')。请参阅w3c规范; w3schools(http://www.w3schools.com/)有一些有用的参考和教程。 – 2009-12-08 18:59:00