简体   繁体   English

HTML,JavaScript:从window.onclick函数获取值

[英]HTML, JavaScript: Get a value out of window.onclick's function

I'm trying to make a webpage with a bunch of canvas elements, each one being initialized with a picture drawn to it, and each one having a "next" button that cycles through a bunch of other files stored server-side. 我正在尝试创建一个包含一堆画布元素的网页,每个画布元素都使用绘制的图片进行初始化,每个页面都有一个“下一步”按钮,该按钮可循环浏览服务器端存储的其他文件。 In order to get this to work I need the next button to be aware of which image is currently displayed in its corresponding canvas element. 为了使它起作用,我需要下一个按钮来了解当前在其相应的canvas元素中显示的图像。 That information can't be stored in the individual button element nor in the canvas element as far as I know, so that leaves me thinking I need a global variable storing all of the canvas elements paired with the name of their currently displayed image--that way when the "next" button is clicked it can look up its canvas element and image, decide what next image should be drawn, and draw it. 据我所知,该信息无法存储在单个按钮元素中,也不能存储在canvas元素中,因此让我觉得我需要一个全局变量来存储所有的canvas元素以及它们当前显示的图像的名称,这样,当单击“下一步”按钮时,它可以查找其画布元素和图像,确定应绘制的下一幅图像,然后绘制它。

However, to find all the canvas elements in the page, I seem to need the window.onload function to make sure I only collect them after the page has fully loaded. 但是,要查找页面中的所有canvas元素,我似乎需要window.onload函数来确保仅在页面完全加载后才收集它们。 However, I can't find a way to get the information that I collect inside of this function, outside and into a global variable. 但是,我找不到在该函数内部,外部和全局变量中获取所收集信息的方法。 I saw one person show how, if you just put stuff in a list that you create outside the function, you can update the contents of the list inside the function--and since this is done by pass-by-reference rather than pass-by-value, the list will be available outside the function and get filled with the appropriate information. 我看到一个人演示了如何将内容放到在函数外部创建的列表中,从而可以在函数内部更新列表的内容-由于此操作是通过按引用传递而不是通过-按值排列,列表将在函数外部可用,并填充有适当的信息。 However, for my particular application, that means that I need to know the length of the list upon creating it ... but the script only gets to learn that inside the window.onload function. 但是,对于我的特定应用程序,这意味着我需要在创建列表时知道列表的长度...但是脚本只能在window.onload函数中了解列表的长度。 So I seem to be in a catch-22. 所以我似乎陷入了困境。

To give a minimal example, below is the code that I would write if I didn't have to specify the length of the array in advance. 举一个最小的例子,下面是我不必预先指定数组长度的情况下要编写的代码。 I use the number 1 to indicate that each canvas starts at the first image (in the rest of my HTML and JavaScript I have a way of pairing canvas elements to numbers so that they find the appropriate image to draw). 我用数字1表示每个画布都从第一个图像开始(在我的HTML和JavaScript的其余部分中,我可以将画布元素与数字配对,以便它们找到合适的图像进行绘制)。

var canList = new Array();

window.onclick = function() {
    var cans = document.getElementsByTag("canvas");
    for (i = 0; i < cans.length; i++) {
        canList[i] = [cans[i],1];
    }
}

The behavior I want is for me to be able to use canList elsewhere in the code. 我想要的行为是我能够在代码的其他地方使用canList Obviously that won't work because I haven't specified the length of canList in the variable declaration, but I can only find that out by first creating the cans variable. 显然,这是行不通的,因为我没有在变量声明中指定canList的长度,但是我只能通过首先创建cans变量才能发现这一点。 If I move the canList variable declaration inside the function, it will no longer be available outside. 如果我将canList变量声明移到函数内部,它将在外部不再可用。

You don't need to define the length of the array on creation. 您无需在创建时定义数组的长度。 Simply say: 简单地说:

var array = new Array();

or 要么

var array = [];

Then you can define any one of the indices as you need with: 然后,您可以根据需要定义以下任一索引:

array[n] = value;

It seems like what you're asking is how to add entries to a Javascript array without having to initialize it to a certain size. 似乎您要问的是如何在不将其初始化为一定大小的情况下向Javascript数组添加条目。 To do that, you can use array.push : 为此,您可以使用array.push

var array = [];
array.push(1);
array.push(2);
array.push(3);
// array will be [1, 2, 3]

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/push

or array.unshift if you want to add it to the beginning of the array: array.unshift如果要将其添加到数组的开头:

var array = [];
array.unshift(1);
array.unshift(2);
array.unshift(3);
// array will be [3, 2, 1]

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/unshift https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/unshift

Arrays in Javascript, despite the name, function like linked lists. 尽管有名称,但Javascript中的数组的功能类似于链表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM